1

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?

2
  • Why are you using "\\args[0]\ instead of putting "\\$server\" ? Commented May 29, 2015 at 19:16
  • @luke - I tried that first and it didn't work. Found this post to use args. stackoverflow.com/questions/7023012/… Commented May 29, 2015 at 19:35

3 Answers 3

2

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.

See: http://blogs.msdn.com/b/powershell/archive/2006/07/15/variable-expansion-in-strings-and-herestrings.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

1 Comment

That's where I had begun but it doesn't work. Cannot find path '\\\\hot.war' because it does not exist. [link]stackoverflow.com/questions/7023012/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.