在安装了Powershell 6的Centos72中运行Powershell脚本时出错

时间:2018-11-26 20:37:44

标签: powershell

我有一个Powershell脚本来删除群集中一堆Windows主机上的文件夹。

在ISE中以及从笔记本电脑本地运行时,它运行良好。但是,我在Centos7.2机器上安装了Powershell,当我尝试运行时,出现以下错误

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可以正常工作是没有意义的。

任何帮助将不胜感激

0 个答案:

没有答案