Reputation: 43
I'm trying to get the last write time on a file from a remote server.
This doesn not work:
$server = "MyServerName"
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$args[0]\hot.war" } -argumentlist $server | select -Property LastWriteTime
This does work:
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\MyServerName\hot.war" } -argumentlist $server | select -Property LastWriteTime
Can anyone help make the first set work?
Upvotes: 1
Views: 1391
Reputation: 952
Another way, if you are using PowerShell 3. You can do something like this:
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {
Get-ChildItem "\\$using:server\hot.war"
} | select -Property LastWriteTime
Upvotes: 1
Reputation: 3616
Be careful with variables in strings: "\\$args[0]\hot.war"
will be expanded to \\MyServerName[0]\hot.war
.
Use "\\$($args[0])\hot.war"
to be sure that $args[0]
will be treated as a single expression.
Upvotes: 2
Reputation: 667
You will want to add the server variable into your first line...
$server = "MyServerName"
$lastWrite = Invoke-Command -Computername $server -ScriptBlock {Get-ChildItem "\\$server\hot.war" } -argumentlist $server | select -Property LastWriteTime
Upvotes: 0