I was trying to execute powershell script from Cmd.
C:\powershell C:\Powershell_Scripts\CheckWebSiteExists.ps1
Error : Get-Content : Cannot find path '\\XXXX\China-team\Release
Package\CustomerDB_file\CustomerDB.txt' because it does not
exist.
Powershell script :
$test = (Get-Content "\\XXXX\China-team\Release Package\\CustomerDB_file\CustomerDB.txt") -split ','
this was running fine in powershell but not in cmd. I have mapped that share path and tried executing, but no luck.
Try This..
powershell -command "$test = (Get-Content "\\XXXX\China-team\Release Package\\CustomerDB_file\CustomerDB.txt") -split ','"
It appears that PowerShell cannot find the filename you have specified. Take a step back and verify that the file is there.
Test-Path '\\XXXX\China-team\Release Package\\CustomerDB_file\CustomerDB.txt'
If that does not return True, try removing the escaped backslash.
Test-Path '\\XXXX\China-team\Release Package\CustomerDB_file\CustomerDB.txt'
If Test-Path cannot return True, then the file cannot be found. There is no point in using Get-Content until the file can be found.
Related
I have a command like this:
powershell -command "Select-String -Path 'C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf' -Pattern 'ServerActive'"
Which outputs:
C:\Program Files\Zabbix Agent 2\zabbix_agent2.conf:5:ServerActive=79.240.122.98
I want to get the "79.240.122.98" part. How can I do that? I think that the best approach would be to split the string by the "=" character and then get the second item, but I did not find the way to do it.
From a .bat file? This works for me.
powershell ((select-string serveractive 'c:\program files\zabbix agent 2\zabbix_agent2.conf') -split '=')[1]
79.240.122.98
I have a powershell script that gets the Java environment variable within the PATH which could be as follows:
$java = (Get-Command java | Select-Object -ExpandProperty Source).toString()
Outputs: $java = C:\Program Files (x86)\Java\jdk1.8.0\bin\java.exe
Then, I set the JAVA_HOME env variable by removing the \bin\java.exe path
$env:JAVA_HOME = "$java" -replace "\\bin\\java\.exe",""
And call a .bat script.
The main problem is that I get an error due to the parenthesis in (x86) within the .bat script:
\Java\jdk1.8.0 was unexpected at this time.
If I change the java path to Program Files (without any parenthesis) everything works fine. So I have been trying to escape special characters in the -replace method but nothing seems to be working.
$env:JAVA_HOME = "$java" -replace "\\bin\\java\.exe","" -replace [regex]::Escape(')'), [regex]::Escape(')') -replace [regex]::Escape('('),[regex]::Escape('(')
Any ideas?
I was trying to write a script to test the availability of the file path. One of the process is that user have to input the file path or just drag the file into the Powershell command prompt.
In this process,
User will input the file path like C:\Program Files\7-Zip\7z.exe and the string will become the value of $filePath variable which will be used as the parameter of the Test-Path command.
But sometime, user will just drag the file into the Powershell command prompt so that the string will have a pair of quotation mark included just like the picture 1.
("C:\Program Files\7-Zip\7z.exe")
Picture 1
Then you will see when I try to test the path using Test-Path command with that $filePath variable while the value(string) of the $filePath included a pair of quotation mark, the result will always be False even though the path is existing and valid.
But when I use the same Test-Path command without using variable (I mean just copy and paste the file path into the command), it works normally.
IT'S WEIRD!
I have tried typing the file path by keyboard into the variable instead of dragging the file into Powershell command prompt. (Without Quotation mark)
Then use the same method to test the filepath (using variable for the file path). It works fine.
Picture 2
I don't get it. Aren't they the same thing?
when
$filePath = "C:\Program Files\7-Zip\7z.exe"
Below 2 commands SHOULD have the same result! WHY they are not?
Test-Path -Path $filePath
Test-Path -Path "C:\Program Files\7-Zip\7z.exe"
In case of drag and drop, it looks like if the path has no spaces it will return true . If it has a space then PowerShell places quotes around it. In that case, PowerShell is literally seeing the path as "C:\Program Files\7-Zip\7z.exe"
What you can do is use the -replace operator like this -
Test-Path -path ($filepath -replace '"', "") -PathType Leaf
OR
As suggested by #Josefz in the comments, you could also use Trim() method like -
Test-Path -path ($filepath.Trim('"')) -PathType Leaf
Not exactly an explanation to your problem, but you could use this as a workaround.
If the user types a filename that contains embedded " characters, then Test-Path will return $false. Why? File names cannot contain the " character; the " character is used by parsers to indicate that an argument in a string contains whitespace. So this will of course return $false:
$filePath = '"C:\Program Files\7-Zip\7z.exe"'
Test-Path $filePath
The embedded " characters are not part of the file name; you need to omit them for the result to be $true.
What version of powershell are you using? I get true for both commands
PS C:\Users> $filePath = "C:\Program Files\7-Zip\7z.exe"
PS C:\Users> Test-Path -Path $filePath
True
PS C:\Users> Test-Path -Path "C:\Program Files\7-Zip\7z.exe"
True
PS C:\Users> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 17134 48
EDIT ----------
Got it, the problem is that Read-Host will save the string literally, with the quotes. The simple solution is to remove the quotes
PS C:\Users> $filePath = Read-Host -Prompt "enter input"
enter input: "C:\Program Files\7-Zip\7z.exe"
PS C:\Users> $filePath
"C:\Program Files\7-Zip\7z.exe"
PS C:\Users> Test-Path -Path $filePath
False
PS C:\Users> Test-Path -Path $filePath.replace("`"","")
True
I'm trying to update file string on remote computer. In my script i'm using following code.
Get-Content Clients.txt | ForEach-Object \\{ Get-Item "\\$_\D$\Runtime\run.properties" \\} | Replace-FileString.ps1 -Pattern '$PropName=.*' -Replacement '$PropName=$PropValue' -Overwrite
But I'm getting the following error:
Replace-FileString.ps1 : The term 'Replace-FileString.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program
I even tried with Replace-FileString and i'm getting same error.
Is this the correct way or is there any other way to do it?
You specify a script name. PS does not find this script, so insert the path (target system) of your script - e.g. "c:....\Replace-FileString.ps1".
If it's in the same dir then just ".\Replace-FileString.ps1".
I tried the following command and it is working perfectly
Get-Content Clients.txt | ForEach-Object { (Get-Content -Path "\\$_\D$\Runtime\run.properties") -replace "provisioner.events.monitor=.*","provisioner.events.monitor=JagadeeshTest" | Set-Content -Path "\\$_\D$\Runtime\run.properties"}
Replace-FileString.ps1 is part of psappdeploytoolkit and will not be present on a system unless you have specifically installed it.
You can instead use replace (more info on this command) instead:
ForEach ($client in (Get-Content Clients.txt)) {
(Get-Content "\\$client\D$\Runtime\run.properties") -replace "$PropName=.*","$PropName=$PropValue" |
Out-File "\\$client\D$\Runtime\run.properties"
}
Putting the following in a Powershell console, it outputs the variable value:
[Environment]::UserName
Putting this:
PS C:\> Test-Path C:\Users\${[Environment]::UserName}\AppData\Local\Microsoft\Outlook
False
Odd, so I went ahead and outputted to the console itself:
PS C:\> Write-Host C:\Users\${[Environment]::UserName}\AppData\Local\Microsoft\Outlook
C:\Users\\AppData\Local\Microsoft\Outlook
It returns blank. Why? Putting the variable directly to console does output its value while passing it like that it doesnt...
You should use $env:LOCALAPPDATA since you don't have to hard code c:\. You should also use the Join-Path cmdlet if you want to join a path:
$path = Join-Path $env:LOCALAPPDATA 'Microsoft\Outlook'
Are you sure the path being output is correct; i.e. that the username's appended? When I run it I see c:\Users\\AppData\...; i.e. no username.
Working code:
$fn = ("C:\Users\{0}\AppData\Local\Microsoft\Outlook" -f $env:Username)
Write-Host $fn
Test-Path $fn