PowerShell script 'cannot find path' when deployed by PDQ
Currently have a PowerShell script that's supposed to get content of the file on a remote computer:
$send = gc \\$env:COMPUTERNAME\c$\$WINDOWS.~BT\Sources\Panther\setupact.log | Select-String uninstall
Obviously, when using PDQ Deploy, the target computer that is chosen will fill in for the $env:COMPUTERNAME variable. However, when the script is deployed by PDQ, it keeps failing wi/ this error:
Get-Content : Cannot find path '\\nameOfComputerHere\c$\.~BT\Sources\Panther\setupact
.log' because it does not exist.
At C:\Windows\AdminArsenal\PDQDeployRunner\service-1\exec\smh.ps1:8 char:15
+ $send = gc <<<< \\$env:COMPUTERNAME\c$\$WINDOWS.~BT\Sources\Panther\setu
pact.log | Select-String uninstall
+ CategoryInfo : ObjectNotFound: (\\nameOfComputerHere\...er\setupact.l
og:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo
ntentCommand
The file definitely exists as if I run this script manually and hard code the remote computer's name, it works just fine. Any help will greatly be appreciated.
Comments
I think you may have a copy/paste error. $WINDOWS does not exist, that's why the evaluated line becomes \.~BT\
>Cannot find path '\\nameOfComputerHere\c$\.~BT\Sources\Panther\setupact.log'
Appreciate the brief response Colby.
I saw that too, but looking at my PowerShell script $WINDOWS is clearly there.
It's odd because for a test, I moved the setupact.log file out of the $WINDOWS.~BT directory and into the C: drive and PDQ deployed it successfully which makes me want to believe there's something up wi/ the $WINDOWS.~BT directory that it doesn't like.
Like I mentioned, when I run it manually, through PowerShell, it works just fine, however I want to use PDQ Deploy because choosing the target makes it easier for filling in the env:COMPUTERNAME variable.
Thoughts?
Can you share the whole script you're trying to run?
Sure thing:
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient("smtp.domain.com")
$msg.From = "e-mail@domain.com"
$msg.ReplyTo = "e-mail@domain.com"
$msg.To.Add("support@domain.com")
$msg.subject = "Upgrade Errors"
$send = gc \\$env:COMPUTERNAME\c$\$WINDOWS.~BT\Sources\Panther\setupact.log | Select-String uninstall
$body = @"
$send
"@
$msg.body += $body
$smtp.Send($msg)
Just wanted to follow up & see if you were able to take a look at the whole script. Let me know. Thanks again !!
Ah, it looks like the folder name actually starts with $. PowerShell interprets anything that starts with $ as a variable, so you have to escape the $ with `.
That worked. Can't thank you enough.