Cannot index into a null array
hi there,
i getting cannot index null array error. run script below.
******************************************************
function check-even ($num) {[bool]!($num%2)}
foreach ($file in (get-childitem -path c:\users\rsimmers\desktop\*.ini)) {
"processing file {0}" -f $file
$newfilename = $file.fullname -replace ".ini", ".txt"
$newcontent = foreach ($line in (get-content $file.fullname)){
$printer = $line -match '\| (.*?)"'
$printername = $matches[1]
$printernum = $printername.split("-")[2]
if((check-even $printernum) -eq $true) {
#even
$line.replace('host="acfile01"','host="vjjjrt02"').replace('host="effile01"','host="vjjjrt02"')
}
else {
#odd
$line.replace('host="acfile01"','host="vshhprt01"').replace('host="acfile01"','host="vshhprt01"')
}
}
}
$newcontent
$newcontent | out-file $newfilename
******************************************************************
processing file \\vfs04\users$\kaendm\desktop\test00e8e627f9c.ini
cannot index null array.
@ c:\powershell\treplaceinivalues.ps1:12 char:9
+ $printername = $matches[1]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ categoryinfo : invalidoperation: (:) [], runtimeexception
+ fullyqualifiederrorid : nullarray
you cannot call method on null-valued expression.
@ c:\powershell\treplaceinivalues.ps1:13 char:9
+ $printernum = $printername.split("-")[2]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ categoryinfo : invalidoperation: (:) [], runtimeexception
+ fullyqualifiederrorid : invokemethodonnull
hi zomba,
please refer script:
foreach ($file in (get-childitem -path d:\*.ini)) { $i=0 "processing file {0}" -f $file $newfilename = $file.fullname -replace ".ini", ".txt" $content = get-content $file.fullname foreach ($line in $content){ #if no match, skip if ($line -match '\| (.*?)"') { write-verbose ($matches | out-string) -verbose $printername = $matches[1] $printernum = $printername.split("-")[2] write-host "the original line $line" write-host "-------------------------------" if((check-even $printernum) -eq $true) { #even $content[$i] = $content[$i].replace('host="acfile01"','host="vjjjrt02"').replace('host="effile01"','host="vjjjrt02"') } else { #odd $content[$i] = $content[$i].replace('host="acfile01"','host="vshhprt01"').replace('host="acfile01"','host="vshhprt01"') } } $i++ } $content|out-file $newfilename } if there else regarding issue, please feel free post back.
best regards,
anna wang
please remember mark replies answers if , unmark them if provide no help. if have feedback technet support, contact tnmff@microsoft.com
Windows Server > Windows PowerShell
Comments
Post a Comment