我有一个获取数据库列表的函数,并允许用户选择他们想要使用的数据库,需要将数组制作成多维数组,因为我需要一个针对数据库名称的文件路径。 / p>
以下是代码:
function GetDBList {
Param ($MoveAll)
$ServerAddress = "."
$DBList = Invoke-Sqlcmd -Query "SELECT name FROM sys.databases WHERE CASE WHEN state_desc = 'ONLINE' THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[settings]', 'U') END IS NOT NULL" -ServerInstance $ServerAddress
$databaseList = @()
$databaseListDoc = @()
$databaseListSelect = @()
$databaseListFinal = @()
for ($i = 0; $i -lt $dbList.count; $i++) {
$databaseList += $dbList.ItemArray[$i]
}
# ----------------- EDIT ------------------------------------------------------
# Gets the document path - Needs to be changed to account for multiple branches
$i = 0
# Beging database selection ----------------------
if ($MoveAll -eq $false) {
# Form creation removed
foreach ($item in $databaseList) {
[void]$listBox.Items.Add($item)
}
# Form creation removed
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
$databaseListSelect += $listBox.SelectedItems
}
foreach ($item in $databaseListSelect) {
$DBPath = Invoke-Sqlcmd -Query "SELECT TOP 1 BDocumentsFolder FROM tblBranchSettings" -ServerInstance $ServerAddress -Database $item
$path = $DBPath.ItemArray[0] -replace ('\Documents', "")
$databaseListFinal += ,@($item, $path)
$i++
}
} else {
foreach ($item in $databaseList) {
$DBPath = Invoke-Sqlcmd -Query "SELECT TOP 1 BDocumentsFolder FROM tblBranchSettings" -ServerInstance $ServerAddress -Database $item
$path = $DBPath.ItemArray[0] -replace ('\Documents', "")
$databaseListFinal += ,@($item, $path)
$i++
}
}
return , $databaseListFinal
}
底部的foreach
块中的else
循环工作正常,当我在函数调用后将其打印出来时,我同时获得数据库名称和文件路径。 if
块中的一个(if
块的末尾)没有,它只打印出数据库的名称。