Ошибка запуска скрипта Powershell в Centos72 с установленным powershell 6 - PullRequest
0 голосов
/ 26 ноября 2018

У меня есть скрипт powershell для удаления папки на нескольких хостах Windows в нашем кластере.

Он отлично работает в ISE и при запуске локально с моего ноутбука.Тем не менее, я установил PowerShell на своем компьютере Centos7.2 и при попытке запустить я получаю следующую ошибку

Join-Path : Cannot process argument because the value of argument "drive" is 
null. Change the value of argument "drive" to a non-null value.
At /home/user/powershell/delete_.file.ps1:11 char:20
+     $newfilepath = Join-Path "\\$computer" "$file"
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Join-Path], 
PSArgumentNullException
+ FullyQualifiedErrorId : 
ArgumentNull,Microsoft.PowerShell.Commands.JoinPathCommand

Test-Path : Cannot bind argument to parameter 'Path' because it is null.
At /home/user/powershell/delete_.file.ps1:12 char:19
+     if (test-path $newfilepath) {
+                   ~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Path], 
ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

Вот код для сценария

$filelist = @("C$\.file")
$computers = Get-Content /home/user/powershell/sw_win.txt

foreach ($file in $filelist)
{
    foreach ($computer in $computers)
    {
    Write-Host -ForegroundColor Yellow "Analysing $computers"

    $newfilepath = Join-Path "\\$computer" "$file"
    if (test-path $newfilepath) {
        Write-Host "$newfilepath Folder exists"
        try
        {
            Remove-Item $newfilepath -Force -recurse -ErrorAction Stop
        }
        catch
        {
            Write-host "Error while deleting $newfilepath on $computer. 
`n$($Error[0].Exception.Message)"

        }
            Write-Host "$newfilepath folder deleted"
    } else {
        Write-Information -MessageData "Path $newfilepath does not exist"
            }
    }
}

Я думаю, что егопотому что $ computer не установлен, но что не имеет смысла, так это то, что он отлично работает с использованием ISE.

Любая помощь будет оценена

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...