shazzaraani
Technical User
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
}
}
}
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
}
}
}