I have the following script code
#[string]$password = $( Read-Host "Input password, please" )
param (
[string]$ReleaseFile = $(throw "-ReleaseFile is required"),
[string]$Destination = $(throw "-Destination is required")
)
function unzipRelease($src, $dst)
{
$shell = new-object -com shell.application
$zip = $shell.NameSpace($src)
foreach($item in $zip.items())
{
$shell.Namespace($dst).copyhere($item)
}
}
# .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination "."
unzipRelease –Src '$ReleaseFile' -Dst '$Destination'
I run the script with: .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination "."
But I keep getting this:
PS C:\Users\Administrator\Documents\Tools> .\deployrelease.ps1 -ReleaseFile ".\deploy.zip" -Destination
The string starting:
At C:\Users\Administrator\Documents\Tools\deployrelease.ps1:19 char:16
+ unzipRelease â? <<<< "Src '$ReleaseFile' -Dst '$Destination'
is missing the terminator: ".
At C:\Users\Administrator\Documents\Tools\deployrelease.ps1:19 char:55
+ unzipRelease â?"Src '$ReleaseFile' -Dst '$Destination' <<<<
+ CategoryInfo : ParserError: (Src `'$ReleaseF...'$Destination`':String) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
I couldn't find the fix as I do not see any problem.
Any help?
Look closely at the two dashes in
unzipRelease –Src '$ReleaseFile' -Dst '$Destination'
This first one is not a normal dash but an en-dash (– in HTML). Replace that with the dash found before Dst.
In my specific case of the same issue, it was caused by not having the Powershell script saved with an encoding of Windows-1252 or UFT-8 WITH BOM.
This can also occur when the path ends in a '' followed by the closing quotation mark.
e.g. The following line is passed as one of the arguments and this is not right:
"c:\users\abc\"
instead pass that argument as shown below so that the last backslash is escaped instead of escaping the quotation mark.
"c:\users\abc\\"
In your script, why are you using single quotes around the variables? These will not be expanded. Use double quotes for variable expansion or just the variable names themselves.
unzipRelease –Src '$ReleaseFile' -Dst '$Destination'
to
unzipRelease –Src "$ReleaseFile" -Dst "$Destination"
This error will also occur if you call .ps1 file from a .bat file and file path has spaces.
The fix is to make sure there are no spaces in the path of .ps1 file.
You can spot the error when using # prefix/suffix with multiline string while you actually have the ending suffix "#.
My script looked like that:
Add-Type #"
public class SomeClass {
...
}"#
and I still got the: The string is missing the terminator: "#.
Message was misleading because all I needed to do was to put "# into new line without any leading space:
Add-Type #"
public class SomeClass {
...
}
"#
my folder contained ' symbol. After I removed it, the issue resolved.
if you're using RHEL, try replacing " with ' - this fixed the error for me
cheers
Related
I am trying an if else condition in powershell using string comparison. I tried as per documentation using -eq operator. But getting below error. Here "Build.Reason" is a predefined variable. Not sure why its looking for cmdlet name for variable.
Write-Host "$(Build.Reason)"
if ($(Build.Reason) -eq "Manual" ) {
$temp = "https://url/api/qualitygates/project_status?&pullRequest=$(Build.Reason)"
Write-Host "Manual"
} else {
Write-Host "CI"
}
Error
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\d7af16d6-ce3e-4dec-a636-9447962fdac4.ps1'"
Manual
Manual : The term 'Manual' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\a\_temp\d7af16d6-ce3e-4dec-a636-9447962fdac4.ps1:7 char:5
+ if (Manual -eq "Manual" ) {
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Manual:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
It looks like $(Build.Reason) is a macro-style value provide by a CI system (it is not a PowerShell construct), which is expanded to become a literal part of the code before PowerShell sees it.
Therefore, if this value is to be treated as a string in the resulting PowerShell code, you need to quote it; e.g.:
if ("$(Build.Reason)" -eq "Manual") { # ...
Note that if there's a chance that $(Build.Reason) expands to a value with embedded " characters, they would have to be escaped as `". Similarly, if the value contains embedded $ chars., single-quoting should be used, which may then require escaping embedded single quotes as ''.
If this escaping cannot be performed at the source, you can use a verbatim here-string:
if (#'
$(Build.Reason)
'# -eq 'Manual') { # ...
Important: The closing '# must always be at the very beginning of the line.
I am still quite new to Powershell, but I would like to add my favourite editor into an Alias in Powershell.
I edited the profile.ps1 in C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 which will run automatically when PowerShells starts.
I tried enter New-Alias np notepad.exe which works perfectly everytime I launch PowerShell.
However, I would like to use Sublime Text 3 as my editor. I followed the instructions in this SO page: How can I write a PowerShell alias with arguments in the middle?
The command line I need for Sublime Text is "C:\Program Files\Sublime Text 3\sublime_text.exe" -n [FirstArg]
Which I come out something like this: function sublime { 'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
It does not work and I got the error like this:
At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:72
+ ... lime { 'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+ ~~
Unexpected token '-n' in expression or statement.
At C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1:5 char:75
+ ... lime { 'C:\Program Files\Sublime Text 3\sublime_text.exe' -n $args }
+ ~~~~~
Unexpected token '$args' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
Any helps would be appreciated. Thanks!
Without being a Sublime user, I suspect this should work:
function Start-Sublime {
param([string]$args)
$limeArgs = [string]::Empty
if ($args -ne $null) {
$limeArgs = $args
}
Start-Process "$env:ProgramFiles\Sublime Text 3\sublime_text.exe" -n $limeArgs
}
Set-Alias lime Start-Sublime
Not the prettiest PowerShell code, but I imagine it will do what you're after. It's a little easier to understand than the cryptic & operator is.
I am trying to create a script to create an ini file via powershell to disable windows UAC.
$functionText = #"`[Options`]
UpdateKey=04/28/2015 12:50:27 AM
WINDOW_LEFT=258
WINDOW_TOP=149
WINDOW_WIDTH=666
WINDOW_HEIGHT=519
WINDOW_MAX=0
BackupDir=C:\Windows\System32
UpdateCheck=1
Language=1033
(App)Sun Java=False
NewVersion=5.05.5176
SkipUAC=1
FinderInclude1=PATH|C:\|*.*|RECURSE
FinderInclude2=PATH|D:\|*.*|RECURSE
FinderIncludeStates=1|1
I see SkipUAC=1
ShowCleanWarning=False
ShowFirefoxCleanWarning=False
WipeFreeSpaceDrives=C:\
RunICS=0
CookiesToSave=*.piriform.com|google.com
"#
New-Item c:\Program Files\Ccleaner\Ccleaner.ini -type file -force -value $functionText
I keep getting Unrecognized token in source text.
At C:\PROGRA~3\BEANYW~1\Scripts\2480_C~1\~SC52F~1.PS1:1 char:17
+ $functionText = <<<< #"[Options]
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnrecognizedToken
I tried adding the escape character around options to see if that would do it - I think the issue is around the word [options]
If you want to use a here-string, put the #" on a line by itself.
$functionText = #"
[Options]
UpdateKey=04/28/2015 12:50:27 AM
WINDOW_LEFT=258
WINDOW_TOP=149
WINDOW_WIDTH=666
WINDOW_HEIGHT=519
WINDOW_MAX=0
BackupDir=C:\Windows\System32
UpdateCheck=1
Language=1033
(App)Sun Java=False
NewVersion=5.05.5176
SkipUAC=1
FinderInclude1=PATH|C:\|*.*|RECURSE
FinderInclude2=PATH|D:\|*.*|RECURSE
FinderIncludeStates=1|1
I see SkipUAC=1
ShowCleanWarning=False
ShowFirefoxCleanWarning=False
WipeFreeSpaceDrives=C:\
RunICS=0
CookiesToSave=*.piriform.com|google.com
"#
New-Item "C:\Program Files\Ccleaner\Ccleaner.ini" -type file -force -value $functionText
The advantage of a here-string is that you don't have to escape anything inside the string. So if there were single or double quotes it wouldn't matter. As long as the literal string '"#' doesn't exist, on a line by itself, inside the ini file code you're safe.
Read more about here-strings.
Also, as shown in the sample above, you need to put quotes around the file path.
This is a simplified version of my function:
function DetectLocalUser($localGroup, $members)
{
$result = net localgroup "$localGroup"
#$members= $members.Replace("\","\\")
if ($result -match $members)
{
return $true
}
else
{
return $false
}
}
To invoke the function I use this example (Typical values I am going to receive):
DETECTLocalUser "test" "iis apppool\userapi"
The parameters are not controlled by me. If they were I would escape directly the second parameter "iis apppool\\userapi"
On execution I have a problem with the \ in the parameter. The exact error is:
parsing "iis apppool\icisapi" - Unrecognized escape sequence \i. At
C:\k\a.ps1:6 char:9
+ if ($result -match $members)
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
I found a workaround by adding #$members= $members.Replace("\","\\") fixes the problem but I am not sure if is the best option.
Is my workaroud acceptable or is there a better way of escaping $members parameter?
[RegEx]::Escape($members)
That will ensure that characters in strings get interpreted as literals and not as part of the RegEx.
To explain further, the -match operator is doing a regular expression match, so the string you pass to it is interpreted as a regular expression.
Backslash happens to be the escape character in a regular expression, so that's where your issue is. Using [RegEx]::Escape() ensures that other characters won't be interpreted, such as [,],.,+,(,),^,$,?,*, etc.
I'm a beginner in powershell and know C# pretty well. I have this command http://www.f2ko.de/programs.php?lang=en&pid=cmd that downloads stuff. I'm writing this script to download all the sgf go games from this url http://www.gogameworld.com/gophp/pg_samplegames.php, and was trying to write a powershell script to do it for me. So I wrote a script:
Get-Content test.txt|
ForEach-Object
{
if($_ -eq "=`"javascript:viewdemogame(`'*.sgf`')`" tit")
{
$filename = $_ -replace '=`"javascript:viewdemogame(`''
$filename = $filename -replace '`')`" tit'
&"(Path)/download.exe" ("http://www.gogameworld.com/webclient/qipu/" + $filename)
}
}
However, when I run the script, I keep getting this error:
Unexpected token '`'' in expression or statement.
At (PATH)\test.ps1:7 char:37
+ $filename = $filename -replace '`' <<<< )'
+ CategoryInfo : ParserError: (`':String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
I've looked at the script lots of times and still can't figure out whats wrong. Thanks.
Try this, read the content of the file as one string and then use the Regex.Matches to get all occurrences of the text contained in the parenthesis:
$content = Get-Content test.txt | Out-String
$baseUrl = 'http://www.gogameworld.com/webclient/qipu/'
[regex]::matches($content,"javascript:viewdemogame\('([^\']+)'\)") | Foreach-Object{
$url = '{0}{1}' -f $baseUrl,$_.Groups[1].Value
& "(Path)/download.exe" $url
}
here's an explanation of the regex pattern (created with RegexBuddy):
javascript:viewdemogame\('([^\']+)'\)
Match the characters “javascript:viewdemogame” literally «javascript:viewdemogame»
Match the character “(” literally «\(»
Match the character “'” literally «'»
Match the regular expression below and capture its match into backreference number 1 «([^\']+)»
Match any character that is NOT a ' character «[^\']+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “'” literally «'»
Match the character “)” literally «\)»
Match the character “"” literally «"»
'{0}{1}' is used with the -f operator to create a string. {0} maps to the first value on the right hand side of the operator (e.g $baseUrl) and {1} is mapped to the second value. Under the hood, PowerShell is suing the .NET String.Format method. You can read more about it here: http://devcentral.f5.com/weblogs/Joe/archive/2008/12/19/powershell-abcs---f-is-for-format-operator.aspx
'')" tit'
The -replace operator takes 2 arguments, comma separated. The first is a regular expression that matches what you want replaced. The second is the string you want to relace that with. You appear to be missing the second argument.