Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot index into a null array error in PowerShell when renaming a folder against a SQL query

Status
Not open for further replies.

shazzaraani

Technical User
Jan 5, 2019
1
GB
Hi, I am trying to rename a directory folder name dependent on a mapping table on SQL server, If the directory folder is matches against the Site ID in the SQL table then this will be renamed against the alpha column in the SQL table see below:

SQL TABLE example: Siteid -AiB3304 Alpha -AIB001
Existing directory name example (matches siteid): C:\Customer\AiB3304
This needs to be renamed to the alpha column name C:\Customer\AIB001I

I have connected to the SQL server using the following script however I get the following error:cannot index into a null array + foreach ($customer in $DataSet.Table[0]) {
+ + CategoryInfo : InvalidOperation: :)) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
,

$Directories brings back the following:

Alpha siteid
----- ------
AIB001 AIB3304


Where am I going wrong? it seems like the dataset is empty.

$SQLServer = "protean" #use Server\Instance for named SQL instances!
$SQLDBName = "proteandb"
$SqlQuery = "Select Alpha, siteid from MigrationStaging.Map.CashSiteId__ProteanAlpha where siteid = 'AiB3304'"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)


$SqlConnection.Close()

clear

$Dataset.Tables[0]


$Directories = Get-ChildItem -Path C:\Customer -Directory

$Directories


foreach ($customer in $DataSet.Table[0]) {
foreach ($directory in $Directories){

if ($customer.siteid = $directory.name){
write-host "Customer with siteid $customer.siteid will have $directory.name renamed to $customer.Alpha"
Rename-Item $directory.name -NewName $customer.Alpha -whatif

}

}
}
 
I don't know SQL, however, I'm wondering if there needs to be a semi-colon at the end of your SQL select statement.

$SqlQuery = "Select Alpha, siteid from MigrationStaging.Map.CashSiteId__ProteanAlpha where siteid = 'AiB3304';"



Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top