我正在尝试在 PowerShell 中运行远程脚本它在本地工作正常,但远程失败

问题描述

我可以在本地服务器上运行以下代码,并且运行完美。当我进入远程会话时,它错误地说“ get-content : 无法找到路径 'Q:\path\pick_me.csv' 因为它不存在.

我也尝试过使用 Enter-PSSession 来运行它;我将代码保存为 .ps1 文件并从 PSSession 运行它,但它也失败说:Get-Content:无法将参数绑定到参数“路径”,因为它为空

Invoke-Command -Session $sess1 -ScriptBlock {
$dataset = Get-ChildItem -path "Q:\path\" *.csv | Where-Object {$_.Name -match "pick_me"} | sort-object -Property LastWriteTime |select -Last 1
$findstr = get-content $dataset | %{$_ -match "random_string"}}
$Timeout = 30
$timer = [Diagnostics.Stopwatch]::StartNew()
while (($timer.Elapsed.TotalSeconds -lt $Timeout)) {
    Do {
        $findstr = get-content $dataset | %{$_ -match "random_string"}
        Start-Sleep -Seconds 30

    }
    Until ($findstr -contains $true -or $Timeout -eq 30)
}

    if ($findstr -contains $true){
        Write-Host "The services will Now be restarted" -ForegroundColor Green
        $timer.Stop()
    }
    Else {
        Write-Host "System stuck.  Please review." -ForegroundColor Red -BackgroundColor Yellow
        Exit
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)