Reputation: 4736
I am trying to run the below against multilpe servers using Powershell 3.0 but for some reason I am getting back that the path does not exist...even though it does?
Any ideas?
CODE:
clear
$computer = Get-Content -path c:\temp\servers.txt
foreach ($computer1 in $computer){
Write-Host $computer1
Get-Content -Path '\\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log' -Tail 10
}
ERROR:
SV191267 Get-Content : Cannot find path '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log' because it does not exist. At C:\Users\gaachm5\AppData\Local\Temp\e4651274-dcab-4a87-95a6-0f11437a7187.ps1:7 char:1 + Get-Content -Path '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rs ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (\$computer1\C$...c\RSCD\rscd.log:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
SV193936 Get-Content : Cannot find path '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log' because it does not exist. At C:\Users\gaachm5\AppData\Local\Temp\e4651274-dcab-4a87-95a6-0f11437a7187.ps1:7 char:1 + Get-Content -Path '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rs ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (\$computer1\C$...c\RSCD\rscd.log:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Upvotes: 0
Views: 24404
Reputation: 1543
The path that can't be found only has one \ on the front of it, a UNC share must begin with two \'s. I can see from your code it looks like you are prefixing the two \'s but it doesn't look like it's being carried over.
I would use double quotes instead of single quotes as already pointed out, if you do that then you should not need to escape the $ character either.
Assuming a file called file.txt exists in the C:\temp folder on the local machine.
This fails:
$computer1 = "localhost"
Test-Path '\\$computer1\c$\temp\file.txt'
This works:
$computer1 = "localhost"
Test-Path "\\$computer1\c$\Temp\file.txt"
Upvotes: 3
Reputation: 60910
try:
"\\$computer1\C`$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log" -Tail 10
In single quotes '
the variable are not expanded but treated as literal as $computer1
, the $
for the $admin share
must be escaped with a backtick `
Upvotes: 4