2015-05-29 84 views
1

我試圖從遠程服務器獲取文件的最後寫入時間。Powershell在ScriptBlock中傳遞參數

這並未不起作用:

$server = "MyServerName" 

$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$args[0]\hot.war" } -argumentlist $server | select -Property LastWriteTime 

這不工作:

$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\MyServerName\hot.war" } -argumentlist $server | select -Property LastWriteTime 

誰能幫助使第一套工作?

+0

爲什麼你使用' 「\\ ARGS [0] \'而不是把'」 \\ $服務器\」' – Luke

+0

@luke - 我想,第一,它沒有工作。找到這篇文章來使用參數。http://stackoverflow.com/questions/7023012/passing-powershell-variables-into-a-scriptblock – Matt616

回答

1

另一種方式,如果你正在使用PowerShell的3.你可以做這樣的事情:

$lastWrite = Invoke-Command -Computername $server -ScriptBlock { 
       Get-ChildItem "\\$using:server\hot.war" 
      } | select -Property LastWriteTime 
+0

This works。Thanks – Matt616

0

你會想在服務器變量添加到您的第一線......

$server = "MyServerName" 

$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$server\hot.war" } -argumentlist $server | select -Property LastWriteTime 
+0

這就是我開始的地方,但它不起作用。 \\\ hot.war'因爲它不存在。 [link] http://stackoverflow.com/questions/7023012/passing-powershell-variables-into-a-scriptblock – Matt616