powershell error-CategoryInfo : InvalidOperation: (split:String) [], RuntimeException - powershell

$Analog_File = "c:\temp\test\mt.txt"
#(Get-Item $Analog_File).length
if((Test-Path(-not $Analog_File)) -or ((Get-Item $Analog_File).length -eq 0kb))
{
exit
}
$m = Select-String -pattern ",T" c:\temp\test\mt_1.txt
#$m.length #Get the number of rows
For ($i=0; $i -le $m.length; $i++)
{
$s1 = $m[$i] -split ":"
$s1[2]
if ([int]$s1[2] -ge 9) #le = less than, ge = greater than, eq = equal to
{
$s2 = $s1[3] -split ","
$compid = $s2[2].split('''')[0] + "." + $s2[3].split('''')[0] + "." + $s2[4].split('''')[0] + "." + $s2[5].split('''')[0]
$matches = (Select-String -pattern $compid c:\temp\test\mt_1.txt).line
#($matches.split(';')[0])
#($matches.split(';')[0]).split(',')[0]
#($matches.split(';')[0]).split(',')[1]
if ($s2[6] -eq "T")
{
#$s2[2].split('''')[1]
#"Not in Service"
#"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!0!" >> $fname
}
if ($s2[7] -eq "T")
{
#"Alarm inhibit"
#"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!1!" >> $fname
}
if ($s2[8] -eq "T")
{
#"Manual update"
#"!" + ($matches.split(';')[0]).split(',')[0] + "!" + ($matches.split(';')[0]).split(',')[1] + "!2!" >> $fname
}
}
}
the error message:
You cannot call a method on a null-valued expression.
At line:17 char:31
$compid = $s2[2].split <<<< ('''')[0] + "." + $s2[3].split('''')[0] + "." + $s2[4].split('''')[0] + "." + $s2[5].split('''')[0]
CategoryInfo : InvalidOperation: (split:String) [], RuntimeException
FullyQualifiedErrorId : InvokeMethodOnNull
what can i do??

Related

Powershell error "you cannot call a method on a null-valued expression"

I need to find the problem in this script
when I executed I got this message in the log
you cannot call a method on a null-valued expression
for( $i = 0; $i -lt $DataSet.Tables[0].Rows.Count; $i++)
{
try{
$valuestr = New-Object -TypeName System.Text.StringBuilder
for ( $x = 0; $x -le 10; $x++)
{
if ($x -lt 10)
{
[void]$valuestr.Append("' " +$DataSet.Tables[0].Rows[$i][$x].ToString().Trim().Replace("'","/")+"',")
}
else
{
[void]$valuestr.Append("' " + $DataSet.Tables[0].Rows[$i][$x].ToString().Trim().Replace("'", "/")+"'")
}
}
$sqlCmdd.CommandText = "INSERT INTO $sqlTable (ICN,ICN_CREATE_DT,ICN_SEQ_NUM,ICN_SUBNUM,USR_REF,USR_REL_REF,MSG_TYP,SW2_SLA_111,SW2_UETR_121,MT097_165) Values (" + $valuestr.ToString() + ")"
$sqlCmdd.ExecuteNonQuery()
}
catch
{
$_.Exception.Message | Out-File C:\musta\logError.txt -Append
}
}
I executed and the error was displayed:
You cannot call a method on a null-valued expression.
At line:13 char:103
[void]$valuestr.Append("' " + $DataSet.Tables[0].Rows[$i][$x].ToString <<<< ().Replace("'", "/")+"'")
CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
FullyQualifiedErrorId : InvokeMethodOnNull
Big Thank for your help Bro Mathias R. Jessen the issue solved.
The inner loop iterates 11 times but you only have 10 columns. Change $x -le 10 to $x -lt 10 in the loop condition, then change if($x -lt 10) to if($x -lt 9) –
Mathias R. Jessen

Adding 100 users in powershell script not working

I am trying to add 100 user accounts with PowerShell in my domain system in Windows. I get an error message called "You cannot call a method on a null-valued expression" when running the script for import.
Error Message:
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Downloads\user-import.ps1:37 char:9
+ $brukernavn = ($fornavn.Substring(0,3) + $etternavn.Substring ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull*
The PowerShell Script:
Import-Module ActiveDirectory
$csvpath = $PSScriptRoot + "\produksjonsbrukereImport.csv"
if (Test-Path $csvpath) {
#$csvpath = "C:\script\produksjonsbrukereImport.csv"
$csv = Import-Csv -Delimiter "," -Path $csvpath -Encoding UTF7
$OUBasePath = ",OU=Produksjon,OU=OpMeis,DC=OpMeis,DC=local"
$logpath = "$PSScriptRoot\import-brukere-loggfil.txt"
}
foreach ($line in $csv) {
$fornavn = $line.fornavn
$etternavn = $line.etternavn
$navn = ($fornavn + " " + $etternavn)
$beskrivelse = $line.beskrivelse
$passord = $line.Passord
$avdeling = $line.avdeling
$brukernavn = ($fornavn.Substring(0,3) + $etternavn.Substring(0,3)).ToLower()
$brukernavn = $brukernavn -replace "æ", "a"
$brukernavn = $brukernavn -replace "å", "a"
$brukernavn = $brukernavn -replace "ø", "o"
$principal = $brukernavn + "#OpMeis.local"
$profPath = ($profBasePath + $brukernavn)
$profPathTrue = $profPath + ".V2"
$OU = ("OU=" + $avdeling + $OUBasePath)
}

Getting txt not found error when running this script

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

Powershell unexpected token

have a great 2013. I am getting my hands wet on my first PS Script and I am stuck on a very common error, the "Unexpected Token" error. I've searched the internet, but could not find an answer, so please help me out.
If ($_.Voorvoegsel.Trim() -ieq "") {
$dn = $_.Voornaam.Trim() + " "
$dn += $_.Achternaam.Trim()
$email = $_.Voornaam.substring(0,1).ToLower() + "."
$email+= $_.Achternaam.Trim().ToLower() + "#test.nl
} Else {
$dn = $_.Voornaam.Trim() + " "
$dn += $_.Voorvoegsel.Trim() + " "
$dn += $_.Achternaam.Trim()
$email = $_.Voornaam.substring(0,1).ToLower() + "."
$email += $_.Voorvoegsel.Replace(" ","").ToLower()
$email += $_.Achternaam.Trim().ToLower() + "#test.nl
}
The strange thing is that it errors on the $_.Voorvoegsel variable in the else statement:
Unexpected token '
$dn += $_.Voorvoegsel.Trim()
$dn += ' in expression or statement.
At C:\Users\Public\Documents\PSImportTest.ps1:42 char:12
+ $dn = $_ <<<< .Voornaam.Trim() + " "
+ CategoryInfo : ParserError: (
$dn += $_....m()
$dn += :String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
Anyone out there who sees what my blind spot is here?
It may be that you are missing an end quote on line 5:
$email += $_.Acternaam.Trim().ToLower() + "#test.nl"
It looks like you also may be missing one on line 12. Powershell may think the rest of the code, until the next quote character, is part of your string literal.

Powershell for SharePoint >> getting we!rd error

I keep getting following error on this code:
#Setup default variables
$webUrl = Get-SPWeb -Identity "http://CiscoIntranet/sites/VOIP"
$list = $webUrl.GetList("http://CiscoIntranet/sites/VOIP/ForwardTech")
[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
function ProcessMove {
param($folderUrl)
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files)
{
[Microsoft.SharePoint.SPFile]$spFile = $file;
$docset=$($file.Counterparty2);
$destinationFolderUrl = "http://CiscoIntranet/sites/VOIP/ForwardTech/" + $docset;
$spFile.MoveTo($destinationFolderUrl + $file.Name, $true);
$webUrl.Update();
}
}
#Move root Files
ProcessMove($list.RootFolder.Url)
You cannot call a method on a null-valued expression.
At C:\PS\MoveFiles.ps1:8 char:28
+ $folder = $web.GetFolder <<<< ($folderUrl)
+ CategoryInfo : InvalidOperation: (GetFolder:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because [Microsoft.SharePoint.SPListItemCollection] doesn't contain a method named 'MoveTo'.
At C:\PS\MoveFiles.ps1:13 char:23
+ $list.Items.MoveTo <<<< ($destinationFolderUrl + $file.Name, $true);
+ CategoryInfo : InvalidOperation: (MoveTo:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
This is the working code...
$siteURL="http://CiscoIntranet/sites/VOIP"
$docLib = "ForwardTech"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$collFiles=$web.GetFolder($docLib).Files
$count=$collFiles.Count
while($count -ne 0)
{
$item = $collFiles[$count-1].Item
$DocSet = $item["Region"]
Write-Host "$DocSet is the doc set. $collFiles[$count-1].Name is name"
$collFiles[$count-1].MoveTo($siteURL + "/" + $docLib + "/" + $DocSet + "/" + $collFiles[$count-1].Name, $true)
$count--
}