Powershell - 文件路径,不被识别为cmdlet的名称。功能脚本文件

时间:2017-06-19 15:20:30

标签: powershell

我收到以下错误,

捕获的错误 - 陷阱:带有消息TRAPPED的System.Management.Automation.RemoteException:术语'D:\ ServiceNow \ RDC- Dev-All \ agent \ scripts \ PowerShell \ ImMigration_script.ps1'无法识别为cmdlet,函数,脚本文件的名称 e,或可操作的程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确并且t 再次。

问题似乎与invoke-command

有关
Invoke-Command -Session $Session -ScriptBlock  $theCommand2

我已经厌倦了使用-FilePath而没有运气。 也厌倦了分别传递命令和参数:

Invoke-Command -Session $Session -ScriptBlock  $theCommand2 -argumentlist $leName

我正在使用:

触发脚本
D:\ServiceNow\RDC-Dev-All\agent\scripts\PowerShell\invokLyncUAdd.ps1 -param1 'CN=lync2013testuser1,CN=Users,DC=test,DC=COMPANY,DC=com' -param2 AD\sys-LyncProATSC -param3 Z0185-XAP0007-S.test.COMPANY.com

############################################### ################################

param( $param1, $param2, $param3 )

$ErrorActionPreference = "Stop"

# trap {
#             write-output $("TRAPPED: " + $_.Exception.GetType().FullName);
#             write-output $("TRAPPED: " + $_.Exception.Message);
#             break
#}

$leName = $param1
$leName = ("'" + "$leName" + "'")

$thePath = 'D:\ServiceNow\RDC-Dev-All\agent\scripts\PowerShell'
$theCommand = $thePath+"\ImMigration_script.ps1 -param1 $leName"
$theCommand2 = [Scriptblock]::Create($theCommand)

# Write-Host "We use string $theCommand below"

$Account = $param2
$useP = Get-Content $thePath\'Information.txt'
$Prompt =  convertto-securestring $useP -AsPlainText -Force
$leHost = $param3
try{

$Credential = new-object -typename System.Management.Automation.PSCredential 
-argumentlist $Account, $Prompt
$Timeout = New-PSSessionOption -IdleTimeout 60000
$Session = New-PSSession -ComputerName $leHost -Credential $Credential -
Authentication Credssp -SessionOption $Timeout -ErrorAction Stop
Invoke-Command -Session $Session -ScriptBlock  $theCommand2 
}
catch
{
    $exceptType = $("TRAPPED: " + $_.Exception.GetType().FullName);
    $exceptMess = $("TRAPPED: " + $_.Exception.Message);

}
finally
{
    if($exceptType) { "Errors caught - $exceptType with message $exceptMess " }    }

任何帮助都会很棒,谢谢

2 个答案:

答案 0 :(得分:2)

会话正在远程计算机上执行,我相信PowerShell期望该文件存在。

我会尝试将本地脚本作为脚本块加载,以便它在内存中来接近它:

$thePath     = 'D:\ServiceNow\RDC-Dev-All\agent\scripts\PowerShell'
$theCommand  = $thePath+"\ImMigration_script.ps1"
$theCommand2 = [Scriptblock]::Create(Get-Content $theCommand)

然后,从你的问题:

Invoke-Command -Session $Session -ScriptBlock  $theCommand2 -argumentlist $leName

如果有效,请告诉我。

答案 1 :(得分:0)

如果文件在本地,那么

powershell.exe -noexit -file 'D:\ServiceNow\RDC-Dev-All\agent\scripts\PowerShell\invokLyncUAdd.ps1' -param1 'CN=lync2013testuser1,CN=Users,DC=test,DC=COMPANY,DC=com' -param2 'AD\sys-LyncProATSC' -param3 'Z0185-XAP0007-S.test.COMPANY.com'

如果它在远程系统中,请确保在invoke-command中正确提及远程路径。