I have the file below, which accepts an array of servers. Works well when there are multiple items in the array, and I note the answer that a user suggested in a previous post, which was to put a , in front of the values if its a single item array, trying that with a hash table just doesnt worked. I have tried various options without much luck.
param ([array[]]$servers = $(throw "Input array of servers.") , $folder )
$x = $servers
$k = 'serverid','servername','locationid','appid' # key names correspond to data positions in each array in $x
$h = #{}
For($i=0;$i -lt $x[0].length; $i++){
$x |
ForEach-Object{
[array]$h.($k[$i]) += [string]$_[$i]
}
}
$all_server_ids = $h['Serverid']
foreach ($server_id in $all_server_ids)
{
$severid = $h["serverid"][$all_server_ids.indexof($server_id)]
$servername = $h["servername"][$all_server_ids.indexof($server_id)]
$locationid = $h["locationid"][$all_server_ids.indexof($server_id)]
Write-Output "This $severid and this $servername and this $locationid"
}
Running the below.
.\test.ps1 -servers ,('72','Server1\DEV2','1.0') -folder "F:\files"
Getting the error
Cannot index into a null array.
+ $servername = $h["servername"][$all_server_ids.indexof($server_i ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
+ $locationid = $h["locationid"][$all_server_ids.indexof($server_i ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
The issue here is that, I need the script to work with multiple items in the array or a single item.
Related
I cannot figure out why the loop below is return null keys.
I think $hashSorted is not of type Hashtable; so how do I force it to be?
Thinking now I need a deep/copy? I just want to sort my hashtable and put it into another hashtable.
This might be a dup of: Sort Hashtable and assign it to a new variable in Powershell
My original questiion was why the keys were null when doing the loop.
Then I realized it probably wasn't a hashtable. Still trying the answers there.
Reference: How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values
cls
$hashFilesAndSizes = #{}
$hashSorted = #{}
$hashFilesAndSizes.Add("file1.txt",1000)
$hashFilesAndSizes.Add("file2.txt",200)
$hashFilesAndSizes.Add("file3.txt",750)
$hashFilesAndSizes.GetEnumerator() | sort value #Displays sorted hash table
Write-Host "Second Try - put in another hashtable"
$hashSorted = $hashFilesAndSizes.GetEnumerator() | sort value #put into a new variable
Write-Host "Original variable in original Order"
$hashFilesAndSizes
Write-Host "Sorted"
$hashSorted #show results
Write-Host "Why loop has null keys?"
foreach($key in $hashSorted.Keys)
{
Write-Host "Key=$key"
#if (-not ([string]::IsNullOrEmpty($key)))
#{
$keyPadded = $key.PadRight(50," ")
$fileSize = $hashSorted[$key]
$fileSizeFormatted = $fileSize.ToString("000,000")
Write-Host "$keyPadded size=$fileSizeFormatted "
#}
}
Write-Host "Test with enumerator"
foreach($item in $hashSorted.GetEnumerator())
{
$key = $hashSorted.Key
Write-Host "Key=$key"
#if (-not ([string]::IsNullOrEmpty($key)))
#{
$keyPadded = $key.PadRight(50," ")
$fileSize = $hashSorted.Value
$fileSizeFormatted = $fileSize.ToString("000,000")
Write-Host "$keyPadded size=$fileSizeFormatted "
#}
}
Results:
> Name Value
>
> ---- ----- file2.txt 200
> file3.txt 750
> file1.txt 1000
> Second Try - put in another hashtable Original variable in original
> Order file3.txt 750
> file1.txt 1000
> file2.txt 200
> Sorted file2.txt 200
> file3.txt 750
> file1.txt 1000
> Why loop has null keys? Key= You cannot call a method on a null-valued
> expression. At C:\Scripts\HashTableSortTest.ps1:23 char:37
> + $keyPadded = $key.PadRight <<<< (50," ")
> + CategoryInfo : InvalidOperation: (PadRight:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull Index operation failed; the array index evaluated to null. At
> C:\Scripts\HashTableSortTest.ps1:24 char:35
> + $fileSize = $hashSorted[ <<<< $key]
> + CategoryInfo : InvalidOperation: (:) [], RuntimeException
> + FullyQualifiedErrorId : NullArrayIndex You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:25 char:50
> + $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
> + CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull
> size=
>Test with enumerator Key= You cannot call a method on a null-valued expression. At C:\Scripts\HashTableSortTest.ps1:38 char:37
> + $keyPadded = $key.PadRight <<<< (50," ")
> + CategoryInfo : InvalidOperation: (PadRight:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:40 char:50
> + $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
> + CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull
> size= Key= You cannot call a method on a null-valued expression. At C:\Scripts\HashTableSortTest.ps1:38 char:37
> + $keyPadded = $key.PadRight <<<< (50," ")
> + CategoryInfo : InvalidOperation: (PadRight:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:40 char:50
> + $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
> + CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull
> size= Key= You cannot call a method on a null-valued expression. At C:\Scripts\HashTableSortTest.ps1:38 char:37
> + $keyPadded = $key.PadRight <<<< (50," ")
> + CategoryInfo : InvalidOperation: (PadRight:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
> C:\Scripts\HashTableSortTest.ps1:40 char:50
> + $fileSizeFormatted = $fileSize.ToString <<<< ("000,000")
> + CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
> + FullyQualifiedErrorId : InvokeMethodOnNull
> size=
This
$hashSorted = $hashFilesAndSizes.GetEnumerator() | sort value
does not produce a new hashtable. Instead, it produces an array of DictionaryEntry objects. Now since $hashSorted is, in fact, not a hashtable, there is no Keys property and indexing by value won't work. To properly construct a new hashtable that preserves key order you have to do
$hashSorted = [ordered] #{}
$hashFilesAndSizes.GetEnumerator() | sort value | foreach {$hashSorted[$_.Key] = $_.Value}
Try to run in new clear powershell context. I suggest your environment has some variables saved in cache.
The only error I see is in the second loop. You're using $hashSorted inside the loop instead of $item. Should be:
$key = $item.Key
....
$fileSize = $item.Value
EDIT: Btw, sorting with GetEnumerator() works just fine.
EDIT 2: $hashSorted.Keys is empty as $hashSorted is not a hashtable, just a collection of key-value pairs.
Write-Host "Test with enumerator"
foreach($item in $hashSorted.GetEnumerator())
{
$key = $item.Key # Not $hashSorted.Key !
Write-Host "Key=$key"
#if (-not ([string]::IsNullOrEmpty($key)))
#{
$keyPadded = $key.PadRight(50," ")
$fileSize = $item.Value # Not $hashSorted.Value !
$fileSizeFormatted = $fileSize.ToString("000,000")
Write-Host "$keyPadded size=$fileSizeFormatted "
#}
}
It turned out that after migration to New Query Language in OMS also WebhookData structure for alert has changed.
I was trying to change my powershell script (which is called at OMS alert via Automation Account runbook) and it works locally for the Input I've copied for some previous (updated) alerts but I cannot get it to work in Automation Account.
Can anyone tell why this don't work in Runbook but works locally?
Here is my runbook input: https://jsonblob.com/adf5e1c2-c948-11e7-af9e-2d30dd548850
I took it from here:
Script:
$WebhookData = '{"WebhookName":"OMS Alert Remediation b64051e5-b9c5-44db-b74f-51d7cf5a9df2","RequestBody":"{\"WorkspaceId\":\"8547d992-7979-46d0-912b-8fffeabe1c8b\",\"AlertRuleName\":\"SRVR slow response - TEST\",\"SearchQuery\":\"ApplicationInsights | where TelemetryType == \\\"Request\\\" and Computer startswith_cs \\\"SRVR\\\" and Computer != \\\"SRVR-DEVEL\\\" | summarize AggregatedValue = avg(RequestDuration) by bin_at(TimeGenerated, 4m, datetime(2017-11-12T10:32:00.0000000)), Computer | sort by TimeGenerated desc\",\"SearchResult\":{\"tables\":[{\"name\":\"PrimaryResult\",\"columns\":[{\"name\":\"TimeGenerated\",\"type\":\"datetime\"},{\"name\":\"Computer\",\"type\":\"string\"},{\"name\":\"AggregatedValue\",\"type\":\"real\"}],\"rows\":[[\"2017-11-12T10:28:00Z\",\"SRVR-06\",1535.2852333333333],[\"2017-11-12T10:24:00Z\",\"SRVR-06\",718.91287857142856]]}]},\"SearchIntervalStartTimeUtc\":\"2017-11-12T10:27:00Z\",\"SearchIntervalEndtimeUtc\":\"2017-11-12T10:32:00Z\",\"AlertThresholdOperator\":\"Greater Than\",\"AlertThresholdValue\":700,\"ResultCount\":2,\"SearchIntervalInSeconds\":300,\"LinkToSearchResults\":\"https://8547d992-7979-46d0-912b-8fffeabe1c8b.portal.mms.microsoft.com/#Workspace/search/index?_timeInterval.intervalEnd=2017-11-12T10%3a32%3a00.0000000Z&_timeInterval.intervalDuration=300&q=ApplicationInsights%20%20%7C%20where%20TelemetryType%20%3D%3D%20%5C%22Request%5C%22%20and%20Computer%20startswith_cs%20%5C%22SRVR%5C%22%20and%20Computer%20!%3D%20%5C%22SRVR-DEVEL%5C%22%20%20%7C%20summarize%20AggregatedValue%20%3D%20avg(RequestDuration)%20by%20bin_at(TimeGenerated%2C%204m%2C%20datetime(2017-11-12T10%3A32%3A00.0000000))%2C%20Computer%20%20%7C%20sort%20by%20TimeGenerated%20desc\",\"Description\":\"W runbook-u testujemy powershell workflow, zamiast powershel script \",\"Severity\":\"Critical\"}","RequestHeader":{"Connection":"Keep-Alive","Accept":"application/json","Host":"s2events.azure-automation.net","User-Agent":"OMS-Remediation","x-ms-request-id":"9be297e0-c196-45c0-ad23-3b513e165648"}}'
$Input = ConvertFrom-Json $WebhookData
$RequestBody = ConvertFrom-Json -InputObject $Input.RequestBody
$Computers = New-Object -TypeName System.Collections.ArrayList
foreach($row in $RequestBody.SearchResult.tables[0].rows)
{
$Computers.Add($row[1]) > $null
}
foreach ($Computer in $Computers | Get-Unique)
{
'Computer: ' + $Computer
Invoke-Command -Credential $c -ComputerName $Computer -ScriptBlock {
$date = Get-Date | Out-File -Append 'C:\tmp\test_log.txt'
}
}
And those are errors in Azure Portal:
1.
ConvertFrom-Json : Invalid JSON primitive: .
At line:9 char:10
+ $Input = ConvertFrom-Json $WebhookData
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
2.
ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
At line:10 char:46
+ $RequestBody = ConvertFrom-Json -InputObject $Input.RequestBody
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJs
onCommand
3.
Cannot index into a null array.
At line:14 char:17
+ foreach($row in $RequestBody.SearchResult.tables[0].rows)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
As you noticed the query language has changed. We have published a new sample here on how to parse results from the new language.
Look here:
https://learn.microsoft.com/en-us/azure/log-analytics/log-analytics-alerts-actions#webhook-actions
Have a look at the new sample and see if you can use that to parse the records.
Thanks,
Anirudh
I get this error when running this script:
Exception calling "Matches" with "1" argument(s): "Value cannot be
null. Parameter name: input" At V:\compiler\shitter2.ps1:3 char:1
+ $tables = $regex.matches((GC v:\compiler\test.txt -raw)).groups.value
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Here is the script code:
$Path = 'V:\compiler\po.htm'
[regex]$regex = "(?s)<TABLE ID=.*?</TABLE>"
$tables = $regex.matches((GC v:\compiler\test.txt -raw)).groups.value
ForEach($String in $tables){
$table = $string.split("`n")
$CurTable = #()
$CurTableName = ([regex]'TABLE ID="
([^"]*)"').matches($table[0]).groups[1].value
$CurTable += ($table[1] -replace "</B></TH><TH><B>",",") -replace "</?
(TR|TH|B)>"
$CurTable += $table[2..($table.count-2)]|ForEach{$_ -replace "</TD>
<TD>","," -replace "</?T(D|R)>"}
$CurTable | convertfrom-csv | export-csv "$CurTableName.csv" -notype
}
You probably forgot the access the entry using [1]:
$tables = $regex.matches((GC v:\compiler\test.txt -raw)).groups[1].value
I am in need of checkingh a larger number of word documents (doc & docx) for a specific text and found a great tutorial and script by the Scripting Guys;
https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/01/find-all-word-documents-that-contain-a-specific-phrase/
The script reads all documents in a directory and gives the following output;
Number of times mentioned
Total word count in all documents where the specific text is found
The directory of all files containing the specific text.
This is all I need, however their code doesn't seem to actually check the headers of any document, which incidentally is where the specific text I'm looking for is located. Any tips & tricks in making the script read header text would make me very happy.
An alternative solution might be to remove the formatting so that the header text becomes part of the rest of the document? Is this possible?
Edit: Forgot to link the script:
[cmdletBinding()]
Param(
$Path = "C:\Users\use\Desktop\"
) #end param
$matchCase = $false
$matchWholeWord = $true
$matchWildCards = $false
$matchSoundsLike = $false
$matchAllWordForms = $false
$forward = $true
$wrap = 1
$application = New-Object -comobject word.application
$application.visible = $False
$docs = Get-childitem -path $Path -Recurse -Include *.docx
$findText = "specific text"
$i = 1
$totalwords = 0
$totaldocs = 0
Foreach ($doc in $docs)
{
Write-Progress -Activity "Processing files" -status "Processing $($doc.FullName)" -PercentComplete ($i /$docs.Count * 100)
$document = $application.documents.open($doc.FullName)
$range = $document.content
$null = $range.movestart()
$wordFound = $range.find.execute($findText,$matchCase,
$matchWholeWord,$matchWildCards,$matchSoundsLike,
$matchAllWordForms,$forward,$wrap)
if($wordFound)
{
$doc.fullname
$document.Words.count
$totaldocs ++
$totalwords += $document.Words.count
} #end if $wordFound
$document.close()
$i++
} #end foreach $doc
$application.quit()
"There are $totaldocs and $($totalwords.tostring('N')) words"
#clean up stuff
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($range) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($document) | Out-Null
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($application) | Out-Null
Remove-Variable -Name application
[gc]::collect()
[gc]::WaitForPendingFinalizers()
EDIT 2: My colleague got the idea to call on the section header instead;
Foreach ($doc in $docs)
{
Write-Progress -Activity "Processing files" -status "Processing $($doc.FullName)" -PercentComplete ($i /$docs.Count * 100)
$document = $application.documents.open($doc.FullName)
# Load first section of the document
$section = $doc.sections.item(1);
# Load header
$header = $section.headers.Item(1);
# Set the range to be searched to only Header
$range = $header.content
$null = $range.movestart()
$wordFound = $range.find.execute($findText,$matchCase,
$matchWholeWord,$matchWildCards,$matchSoundsLike,
$matchAllWordForms,$forward,$wrap,$Format)
if($wordFound) [script continues as above]
But this is met with the following errors:
You cannot call a method on a null-valued expression.
At C:\Users\user\Desktop\count_mod.ps1:27 char:31
+ $section = $doc.sections.item <<<< (1);
+ CategoryInfo : InvalidOperation: (item:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\user\Desktop\count_mod.ps1:29 char:33
+ $header = $section.headers.Item <<<< (1);
+ CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\user\Desktop\count_mod.ps1:33 char:26
+ $null = $range.movestart <<<< ()
+ CategoryInfo : InvalidOperation: (movestart:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\user\Desktop\count_mod.ps1:35 char:34
+ $wordFound = $range.find.execute <<<< ($findText,$matchCase,
+ CategoryInfo : InvalidOperation: (execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Is this the right way to go or is it a dead end?
if you want the header text, you can try the following:
$document.content.Sections.First.Headers.Item(1).range.text
For anyone looking at this question in the future: Something isn't quite working with my code above. It seems to return a false positive and puts $wordFound = 1 regardless of the content of the document thus listing all documents found under $path.
Editing the variables within Find.Execute doesn't seem to change the outcome of $wordFound. I believe the problem might be found in my $range, as it is the only place I get errors in while going through the code step by step.
Errors listed;
You cannot call a method on a null-valued expression.
At C:\Users\user\Desktop\Powershell\count.ps1:24 char:58
+ $range = $document.content.Structures.First.Headers.Item <<<< (1).range.Text
+ CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "MoveStart" with "0" argument(s): "The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"
At C:\Users\user\Desktop\Powershell\count.ps1:25 char:26
+ $null = $range.MoveStart <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodCOMException
You cannot call a method on a null-valued expression.
At C:\Users\user\Desktop\Powershell\count.ps1:26 char:34
+ $wordFound = $range.Find.Execute <<<< ($findText,$matchCase,
+ CategoryInfo : InvalidOperation: (Execute:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The above gives:
PS C:\EndurAutomation\powershell\bin> C:\EndurAutomation\powershell\bin\ets_update_constring.ps1
Exception calling "FindAll" with "0" argument(s): "An operations error occurred.
"
At C:\EndurAutomation\powershell\bin\ets_update_constring.ps1:20 char:30
+ $result = $ldapSearch.FindAll <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
$ldapDN = "dc=<masked>,dc=<masked>"
$ldapURI = "ldap://<masked>/$ldapDN"
$env = "sqlplus -S <masked> ``#env.sql > env.list"
Invoke-Expression $env
$envData = (Get-Content "env.list")
$envFilter = "(|"
foreach ($env in $envData) {
$envFilter += "(cn=$env)"
}
$envFilter += ")"
$ldapEntry = New-Object System.DirectoryServices.DirectoryEntry($ldapUR, $null, $null, [System.DirectoryServices.AuthenticationTypes]::Anonymous)
$ldapSearch = New-Object System.DirectoryServices.DirectorySearcher($ldapEntry)
$ldapSearch.PageSize = 1000
$ldapSearch.Filter = $envFilter
$result = $ldapSearch.FindAll($envFilter)
You already set $ldapSearch.Filter = $envFilter so you don't need to call FindAll by passing in the filter again. Try doing this instead as your very last line of code, as it will still have your filter built into it:
$result = $ldapSearch.FindAll()
I think it's a typo:
$ldapEntry = New-Object System.DirectoryServices.DirectoryEntry(
**$ldapUR**, $null, $null,
[System.DirectoryServices.AuthenticationTypes]::Anonymous
)
Try
$ldapURI
instead of
$ldapUR