×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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

}

}
}

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close