How to fix powershell expression which causes error later in the code - powershell

I am trying to follow this example in order to attach images to an email with powershell. Here is the part of the code that behaves strange:
if ($DirectoryInfo) {
foreach ($element in $DirectoryInfo) {
$failedTest = $element| Select-Object -Expand name
$failedTests += $failedTest
$failedTestLog = "$PathLog\$failedTest.log"
$logContent = [IO.File]::ReadAllText($failedTestLog)
$imageDir = "$PathLog\$element\Firefox\*"
$imageSearch = Get-ChildItem -Path $imageDir -Include *.png -Recurse -Force
$imageFullname = $imageSearch | select FullName | Select-Object -Expand Fullname
$imageFilename = $imageSearch | Select-Object -Expand name
$imageFilename
$imageFullname
# *** THE FOLLOWING LINE CAUSES THE ERROR ***
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $imageFullname.ToString() # *** CAUSING ERROR ***
#$attachment.ContentDisposition.Inline = $True
#$attachment.ContentDisposition.DispositionType = "Inline"
#$attachment.ContentType.MediaType = "image/jpg"
#$attachment.ContentId = '$imageFilename'
#$msg.Attachments.Add($attachment)
$outputLog += "
********************************************
$failedTest
********************************************
$logContent
"
}
} else {
$outputLog = '** No failed tests **'
}
# Create the Overview report
$outputSummary = ""
foreach ($element in $scenarioInfo) {
if (CheckTest $failedTests $element) {
$outputSummary += "
$element : FAILED" # *** ERROR LINE ***
} Else {
$outputSummary += "
$element : Passed"
}
}
If I comment out the line which defines the attachment, the code works fine. If I use the code as it is, I get the following error:
Unexpected token ':' in expression or statement.
At D:\Testing\Data\Powershell\LoadRunner\LRmain.ps1:112 char:11
+ $element : <<<< FAILED"
+ CategoryInfo : ParserError: (::String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
which refers to the line at the bottom of the script where it says "ERROR LINE". What the heck is going on? The behavior look completely illogical to me! I don't understand how a statement, which has no effect at all, can cause an error elsewhere! What is the problem and how to fix it...?
Also it does not matter if I use $imageFullname or $imageFullname.ToString() in the offending line.

Try to replace "$element : FAILED" by
"$element` : FAILED"
The reverse quote will escape the semicolon; which has a specific meaning in PowerShell. (It allows to output subproperty : $env:username for example)

Define the $outputSummary as Array:
$outputSummary = #()
instead of
$outputSummary = ""

Related

Powershell Global Variable usage as parameter to argument

$global:af_fp = "C:\Path\to\folder\"
Function function-name {
do things …
$global:af_fp = $global:af_fp + $variableFromDo_things + "_AF.csv"
}
function-name | ConvertTo-CSV -NoTypeInformation | Add-Content -Path $($af_fp)
Above is the generalized (and abbreviated) script contents for a powershell script.
Every time I run the script in this way, I get the following error:
Add-Content : Could not find a part of the path 'C:\Users\timeuser\Documents\'.
At C:\Users\timeuser\Documents\get_software.ps1:231 char:51
+ ... ware | ConvertTo-CSV -NoTypeInformation | Add-Content -Path $($af_fp)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\timeuser\Documents\:String) [Add-Content], DirectoryNotFoundException
+ FullyQualifiedErrorId : GetContentWriterDirectoryNotFoundError,Microsoft.PowerShell.Commands.AddContentCommand
When I run
Get-Variable -Scope global
after running the script and seeing the error, the variable af_fp contains exactly the information I am seeking for the file name, however, the error shows the variable contents ending in ':String'.
To confuse me even more, if I comment out the lines containing '$global:...' and re-run the same script, IT ACTUALL RUNS AND SAVES THE FILE USING THE LINE
function-name | ConvertTo-CSV -NoTypeInformation | Add-Content -Path $($af_fp)
AS INTENDED. Of course, I had to run the script and watch it error first, then re-run the script with the global variable declaration and update commented out for it to actually work. I want to run the script ONCE and still get the same results.
FYI, I am a complete noob to powershell, but very familiar with the concept of variable scope.....but why is this global not working when initially created and updated, but then work the second time around, when, as far as I can tell, the CONTENT AND SCOPE of the global remains the same...…. any assistance to finding a solution to this small issue would be greatly appreciated; I have tried sooooo may different methods from inquiries through here and on Google...…..
EDIT: not sure why this will matter, because the script ran before as intended when I explicitly typed the parameter for -Path as 'C:\path\to\file'. The ONLY CHANGES MADE to the original, working script (below) were my inclusion of the global variable declaration, the update to the contents of the global variable (near the end of the function), and the attempt to use the global variable as the parameter to -Path, that is why I omitted the script:
'''
$global:af_fp = "C:\Users\timeuser\Documents\"
Function Get-Software {
[OutputType('System.Software.Inventory')]
[Cmdletbinding()]
Param(
[Parameter(ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
[String[]]$Computername = $env:COMPUTERNAME
)
Begin {
}
Process {
ForEach ($Computer in $Computername) {
If (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
$Paths = #("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", "SOFTWARE\\Wow6432node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")
ForEach ($Path in $Paths) {
Write-Verbose "Checking Path: $Path"
# Create an instance of the Registry Object and open the HKLM base key
Try {
$reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine', $Computer, 'Registry64')
}
Catch {
Write-Error $_
Continue
}
# Drill down into the Uninstall key using the OpenSubKey Method
Try {
$regkey = $reg.OpenSubKey($Path)
# Retrieve an array of string that contain all the subkey names
$subkeys = $regkey.GetSubKeyNames()
# Open each Subkey and use GetValue Method to return the required values for each
ForEach ($key in $subkeys) {
Write-Verbose "Key: $Key"
$thisKey = $Path + "\\" + $key
Try {
$thisSubKey = $reg.OpenSubKey($thisKey)
# Prevent Objects with empty DisplayName
$DisplayName = $thisSubKey.getValue("DisplayName")
If ($DisplayName -AND $DisplayName -notmatch '^Update for|rollup|^Security Update|^Service Pack|^HotFix') {
$Date = $thisSubKey.GetValue('InstallDate')
If ($Date) {
Try {
$Date = [datetime]::ParseExact($Date, 'yyyyMMdd', $Null)
}
Catch {
Write-Warning "$($Computer): $_ <$($Date)>"
$Date = $Null
}
}
# Create New Object with empty Properties
$Publisher = Try {
$thisSubKey.GetValue('Publisher').Trim()
}
Catch {
$thisSubKey.GetValue('Publisher')
}
$Version = Try {
#Some weirdness with trailing [char]0 on some strings
$thisSubKey.GetValue('DisplayVersion').TrimEnd(([char[]](32, 0)))
}
Catch {
$thisSubKey.GetValue('DisplayVersion')
}
$UninstallString = Try {
$thisSubKey.GetValue('UninstallString').Trim()
}
Catch {
$thisSubKey.GetValue('UninstallString')
}
$InstallLocation = Try {
$thisSubKey.GetValue('InstallLocation').Trim()
}
Catch {
$thisSubKey.GetValue('InstallLocation')
}
$InstallSource = Try {
$thisSubKey.GetValue('InstallSource').Trim()
}
Catch {
$thisSubKey.GetValue('InstallSource')
}
$HelpLink = Try {
$thisSubKey.GetValue('HelpLink').Trim()
}
Catch {
$thisSubKey.GetValue('HelpLink')
}
$Object = [pscustomobject]#{
#Potential Candidate for AssetID in the TIME system
AssetID = $Computer
#String that contains word or word combinations for the product field of CPE WFN; may also contain the valid values necessary for update, edition, language, sw_edition, target_hw/sw fields as well.
cpeprodinfo = $DisplayName
cpeversion = $Version
InstallDate = $Date
cpevendor = $Publisher
UninstallString = $UninstallString
InstallLocation = $InstallLocation
InstallSource = $InstallSource
HelpLink = $thisSubKey.GetValue('HelpLink')
EstimatedSizeMB = [decimal]([math]::Round(($thisSubKey.GetValue('EstimatedSize') * 1024) / 1MB, 2))
}
$Object.pstypenames.insert(0, 'System.Software.Inventory')
Write-Output $Object
}
}
Catch {
Write-Warning "$Key : $_"
}
}
}
Catch { }
$reg.Close()
}
}
Else {
Write-Error "$($Computer): unable to reach remote system!"
}
$global:af_fp = $global:af_fp + $Computer + "_AF.csv"
}
}
}
Get-Software | ConvertTo-CSV -NoTypeInformation | Add-Content -Path $($af_fp)
'''
IGNORE FORMATTING PLEASE- HAD TROUBLE MAKING INDENTS CORRECTLY FROM COPY-PASTE AND RESTRICTIONS ON SITE FOR CODE BLOCKS.....
NOTE: the ONLY changes I made, that I am asking about, are the global declaration, the global variable update in the function, and the attempt to use the global variable for the -Path parameter....script otherwise runs and will even run WITH THE LAST LINE AS IS if I ran it and errored the first time.....not sure how the addition script will help in any way, shape, or form!
With a little effort, Nasir's solution worked! HOWEVER, I ran across a sample file that had a way of adding to a parameter that inspired me to make a change to my ORIGINAL, that also worked: remove global variable from script entirely and add this code the very end:
$file_suffix = '_AF.csv'
Get-Software | ConvertTo-CSV -NoTypeInformation | Add-Content -Path $env:COMPUTERNAME$file_suffix
In this way, I was able to accomplish exactly what I was setting out to do! Thanks Nasir for your response as well! I was able to also make that work as intended!
Global variables are generally frowned upon, since they often lead to poor scripts, with hard to debug issues.
It seems like your function returns some stuff, which you need to write to a file, the name of which is also generated by the same function. You can try something like this:
function function-name {
param($PathPrefix)
#do things
[pscustomobject]#{"DoThings_data" = $somevariablefromDoThings; "Filename" = "$($PathPrefix)$($variableFromDo_Things)_AF.csv"}
}
function-name -PathPrefix "C:\Path\to\folder\" | Foreach-Object { $_.DoThings_data | Export-Csv -Path $_.Filename -NoTypeInformation }
Or just have your function write the CSV data out and then return the data if you need to further process it outside the function.
Edit: this is just me extrapolating from partial code you have provided. To Lee_Dailey's point, yes, please provide more details.

New-NetIPAddress argument "-IPAddress" causing "Missing closing '}' in statement block or type definition" error

I am trying to configure my local system's virtual switch from a config.json file that looks like this:
{
"system_name":"demo_system",
"version":"0.0",
"network_setting":{
"virtual_switch":{
"internal":{
"NATSwitch":{
"ip_address":"123.456.789.2",
"interface_alias":"vEthernet (NATSwitch)",
"internal_ip_interface_address_prefix":"123.456.789.0/24"
},
"Robot Switch":{
"ip_address":"987.654.321.2",
"interface_alias":"vEthernet (Robot Switch)",
"internal_ip_interface_address_prefix":"987.654.321.0/24"
}
}
}
}
}
The ps1 file testing.ps1 that reads this file and sets up the virtual switch is this:
Param
(
[string]$system_config = "C:\workspace\config.json"
)
function assert-virtual-switch
{
# $args[0] is the $networking_ht
Write-Output "Checking the virtual switches"
$ht = $args[0]
$internal_switch_psobject = $ht["virtual_switch"].internal
$internal_switch_ht = #{}
$internal_switch_psobject.psobject.properties | Foreach-Object { $internal_switch_ht[$_.Name] = $_.Value }
foreach ($internal_switch_key in $internal_switch_ht.Keys) {
if (Get-VMSwitch | Where-Object {$_.Name -eq $internal_switch_key}) {
# The switch already exist
Write-Output "Internal Switch $internal_switch_key exists"
}else{
# Create the virtual switch
Write-Output "Internal Switch $internal_switch_key doesn't exist, creating it now"
$internal_switch_ip = $internal_switch_ht[$internal_switch_key].ip_address
$interface_alias = $internal_switch_ht[$internal_switch_key].interface_alias
$internal_ip_interface_address_prefix = $internal_switch_ht[$internal_switch_key].internal_ip_interface_address_prefix
[byte]$prefix_length = 24
New-VMSwitch –SwitchName $internal_switch_key –SwitchType Internal
New-NetIPAddress -PrefixLength $prefix_length –IPAddress "$internal_switch_ip" -InterfaceAlias "$interface_alias"
New-NetNat –Name $internal_switch_key –InternalIPInterfaceAddressPrefix $internal_ip_interface_address_prefix
}
}
}
$sys_ps_object = Get-Content $system_config | Out-String | ConvertFrom-Json
$sys_ht = #{}
$sys_ps_object.psobject.properties | Foreach-Object { $sys_ht[$_.Name] = $_.Value }
# At this point, the system config is converted to a hash table, we can read from it like such:
foreach($sys_config_key in $sys_ht.Keys){
if ($sys_config_key -eq "network_setting"){
# $networking_ht is a hashtabe of the networking setting
$networking_ps_object = $sys_ht[$sys_config_key]
$networking_ht = #{}
$networking_ps_object.psobject.properties | Foreach-Object { $networking_ht[$_.Name] = $_.Value }
}
}
assert-virtual-switch $networking_ht
As you can see, I've created a function assert-virtual-switch that makes sure the virtual switch exists locally and will create one if it doesn't exist. The input of this function is a hash table of the internal switch information extracted from config.json.
However, I've been running into this error:
Error
At C:\workspace\test\test_new_ipaddress\testing.ps1:39 char:69
+ ... "External switch $external_switch_key doesn't exist, creating it now"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: '.
At C:\workspace\test\test_new_ipaddress\testing.ps1:20 char:14
+ }else{
+ ~
Missing closing '}' in statement block or type definition.
At C:\workspace\test\test_new_ipaddress\testing.ps1:16 char:64
+ foreach ($internal_switch_key in $internal_switch_ht.Keys) {
+ ~
Missing closing '}' in statement block or type definition.
At C:\workspace\test\test_new_ipaddress\testing.ps1:6 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
I've pinpointed the problem to this line that creates and configures the ip address.
New-NetIPAddress -PrefixLength $prefix_length –IPAddress "$internal_switch_ip" -InterfaceAlias "$interface_alias"
If I don't include the -IPAddress argument like this:
New-NetIPAddress -PrefixLength $prefix_length -InterfaceAlias "$interface_alias"
the error doesn't show up anymore:
PS C:\workspace> .\testing.ps1
Checking the virtual switches
Internal Switch Robot Switch exists
Internal Switch NATSwitch exists
So it must be coming from how the cmdlet interprets the IP address since I've tried with and without the quotation mark around $internal_switch_ip.
On another note, if I run the code line by line directly in the Powershell Console, it works.
Does anyone have any ideas of why this happens?
EDITS
As per the solution #Lee_Daily provided, the problem was caused by having a mix of dash and em-dash. After I changed all my "–" to "-", the code works.

PSCustomObject not liking inline if statements in NoteProperty's value

Not sure if I am doing something stupid or if it is a "feature" of PowerShell.
Taking the following example code snippet:
[array]$Strings = #(
"This is an example string"
"This another example string test"
"This is something else fun"
"Not in list"
)
$oData = #()
Foreach($string in $strings)
{
$split = if($string.substring(0,4) -eq "This"){$String.Split(" ")}
$oData += [pscustomobject]#{
Part1 = $Split[0]
Part2 = $Split[1]
Part3 = $Split[2]
Part4 = $split[3]
Part5 = $split[4]
}
}
$oData
This throws the error Cannot index into a null array which is expected as the fourth member of the array "Strings" is not in the list so it cannot be indexed. Fair enough. To mitigate this, I though of the following modification:
$oData = #()
Foreach($string in $strings)
{
$split = if($string.substring(0,4) -eq "This"){$String.Split(" ")}
$oData += [pscustomobject]#{
Part1 = if($Split){$split[0]}
}
}
Which works, until I go to add Part2 to the object:
$oData = #()
Foreach($string in $strings)
{
$split = if($string.substring(0,4) -eq "This"){$String.Split(" ")}
$oData += [pscustomobject]#{
Part1 = if($Split){$split[0]}
Part2 = if($Split){$split[1]}
}
}
ISE underlines "Part2" with the message Unexpected token 'Part2' in expression or statement and the last curly brace of "Part1" has a an underline with the message The hash literal was incomplete.
When I run the script, the error is:
At line:13 char:38
+ Part1 = if($Split){$split[0]}
+ ~
The hash literal was incomplete.
At line:14 char:9
+ Part2 = if($Split){$split[1]}
+ ~~~~~
Unexpected token 'Part2' in expression or statement.
At line:16 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : IncompleteHashLiteral
To me, it seems a valid way to handle the null array and I'm sure I've used if statements in PSCustomObject values before.
I can work around the problem as I've done in the past when I've come across this problem before but I was wondering if anyone can shed light on why PowerShell doesn't like it.
I'm not entirely certain of the reason, but if you end your lines (all but the last so just the first one here) with semicolon ; it works fine. You can of course end all of them in ; for consistency as well.
I speculate it has something to do with the way the parser handles these, and it just doesn't know that the expression has ended for sure, whether it should know or not.
$oData += [pscustomobject]#{
Part1 = if($Split){$split[0]};
Part2 = if($Split){$split[1]}
}
The easy answer seems to be to add the else. As in:
$oData = #()
Foreach($string in $strings)
{
$split = if($string.substring(0,4) -eq "This"){$String.Split(" ")}
$oData += [pscustomobject]#{
Part1 = if($Split){$split[0]}else{$null}
Part2 = if($Split){$split[1]}else{$null}
Part3 = if($Split){$split[2]}else{$null}
Part4 = if($Split){$split[3]}else{$null}
Part5 = if($Split){$split[4]}else{$null}
}
}
Strange that PS is happy with:
if($something -eq $true)
{
"Hello"
}
Without the else or elseif part outside of the [pscustomobject].

Advanced Powershell Error Handling for Microsoft Modules (eg Lync)

I have a fairly complex script that includes Lync functionality. For this I import the PSSession.
When one of these commands errors, my error handling returns the line and command within the Lync module it fails on (eg line 2055 $steppablePipeline.End() ) instead of the line and command in my code (eg line 18 Enable-CSUser)
Is there some way to capture my calling command and line?
The following is a simplified example of the script
Function Main
{
Try
{
LyncTest
MailboxTest
}
Catch {$rtn = ReturnErrorCatch $_ ; Return $rtn} #Return Failure
}
Function MailboxTest
{
$script:callline = (Get-PSCallStack)[1].ScriptLineNumber
Get-Mailbox "kfsjlxghkjsdh" -ErrorAction:Stop
}
Function LyncTest
{
$script:callline = (Get-PSCallStack)[1].ScriptLineNumber
Enable-CSUser "kfsjlxghkjsdh" -ErrorAction:Stop
}
Function ReturnErrorCatch ($catch)
{
If ($catch.InvocationInfo.InvocationName -eq "throw"){$errorreturn = $catch.Exception.Message.ToString()}
Else
{
$line = "Line: " + $catch.InvocationInfo.ScriptLineNumber.ToString()
$exception = $catch.Exception.Message.ToString() -replace "`n"," "
$command = "Command: " + ($catch.InvocationInfo.Line.ToString() -replace "`t","").Trim()
$activity = " (Activity: " + $catch.CategoryInfo.Activity +")"
$line = $line + "(" + $callline.ToString() + ")"
$errorreturn = $line + " " + $exception + "`r`n" + $command + $activity
}
$Output = New-Object PSObject -Property #{Result=1;ResultReason=$errorreturn}
Return $Output
}
$result = Main
$result | fl
This returns:
Result : 1
ResultReason : Line: 2055(5) If SIP address type (SipAddressType) is set to None or is not set, you must specify the SIP address (SipAddress).
Command: $steppablePipeline.End() (Activity: Enable-CsUser)
Within my script, I am able to get the calling line by putting the $script:callline line within every subfunction (better ways to do this?), so if I comment out the LyncTest, and run the mailboxtest, I get the expected error return with erroring command, the line it failed on, and the line the function was called from.
As this is for Exchange 2010, I cannot use any Powershell V3 functionality, it must be V2.

String comparison from XML failing

I am currently working on a project in PowerShell. The project downloads the NVD database XML, loops through a separate CSV for scan results from Nexpose, and pulls CVSS scores for each vulnerability identified with a CVE number.
It seems that matching the CVE number from the sheet (string) with the CVE number in the XML (also a string) is failing completely. The code i am using is below:
clear
[xml]$nvdxml = (New-Object system.Net.WebClient).DownloadString("http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-recent.xml")
$nsmgr = New-Object System.XML.XmlNamespaceManager($nvdxml.NameTable)
$nsmgr.AddNamespace('xsi','http://www.w3.org/2001/XMLSchema-instance')
$nsmgr.AddNamespace('vuln','http://scap.nist.gov/schema/vulnerability/0.4')
$nsmgr.AddNamespace('cvss','http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-recent.xml')
$nsmgr.AddNamespace('df','http://scap.nist.gov/schema/feed/vulnerability/2.0')
$nvdxml.SelectNodes('//vuln:product',$nsmgr) | out-null
$nvdxml.SelectNodes('//vuln:vulnerable-configuration',$nsmgr) | out-null
$nvdxml.SelectNodes('//vuln:vulnerable-software-list',$nsmgr) | out-null
$nvdxml.SelectNodes('//default:nvd',$nsmgr) | out-null
$nvdxml.SelectNodes('//default:entry',$nsmgr) | out-null
$x = import-csv "test-report.csv"
$items = #()
$x | where {$_."Vulnerability Test Result Code" -like "v*"} | %{
$item = #{}
$vid = $_."Vulnerability CVE IDs"
$entry = ""
$item["Vname"] = $_."Vulnerability Title"
$item["VNode"] = $_."Asset IP Address"
$item['VID'] = $vid
$entry = $nvdxml.nvd.entry | where { $_."cve-id" -eq $vid }
$item['Score'] = $entry.cvss.base_metrics.score
$items += $item
}
$items
The $items array contains a vulnerability which has a CVE ID, but the string comparison is utterly failing. When I query for the object's type I get
You cannot call a method on a null-valued expression.
At line:25 char:19
+ $entry.GetType <<<< ()
+ CategoryInfo : InvalidOperation: (GetType:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
When I assign a CVE ID to a string, and attempt to get the relevant vulnerability from the XML for that string, the comparison returns no results; yet, when I replace the variable with the quoted string of the same ID, the query returns the correct result. So, this would fail
$cveID = "CVE-2003-1567"
$nvdxml.nvd.entry | where { $_."cve-id" -eq $cveID }
However, this works fine
$nvdxml.nvd.entry | where { $_."cve-id" -eq "CVE-2003-1567" }
Any ideas? I have tried explicitly casting both $_."cve-id" and $cveID as String with the same results.
I would put all the entries in a hashtable, then look it up via the CVE ID:
$entriesByID = #{ }
$nvdxml.nvd.entry |
ForEach-Object { $entriesByID[$_.id] = $_ }
Note that instead of using the cve-id element, I'm using the id attribute on the event element.
Then, you can look it each entry in the hashtable:
$entry = $entriesByID[$vid]
If you're married to your original approach, you may be running into namespace issues. I would try using SelectNodes instead of PowerShell's virtual XML object properties:
$entry = $nvdxml.SelectSingleNode('/nvd/entry[vuln:cve-id = "$vid"]')
# or
$entry = $nvdxml.SelectSingleNode('/nvd/entry[#id = "$vid"]')