Powershell ISE设置数据集返回的路径

时间:2017-06-01 17:15:52

标签: powershell

我正在尝试设置我要从数据库中的字段导航到的路径。然而;我一直遇到有关|的错误Format-Table命令。删除格式表不允许显示数据集,因此我需要将其保留在那里。当我写入Host $ Path时,返回正确的值但是如果我尝试设置位置$ Path,那是我收到错误的时候:

The object of Type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. 
This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting.

我已经包含了我正在使用的代码。对不起,如果这最终变得简单,我对Powershell了解。

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$DataSet = New-Object System.Data.DataSet
$SqlConnection.ConnectionString = "MyConnection"

cls

$SQLQuery = "SELECT setting, value FROM Table"


$SqlConnection.Open()
$SqlCmd.CommandText = $SQLQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Format-Table -Autosize
$SqlConnection.Close()

    foreach ($Row in $dataset.Tables[0].Rows) 
{ 

    IF ("$($Row.setting)" -eq "ArchiveDir" -and "$($Row.value)" -like "\\*")
    {
            $Path = "$($Row.value)"
            $Path
            set-location $Path
            dir

    }
    ELSE {$Path = ""}

}

1 个答案:

答案 0 :(得分:1)

不确定我是否完全听从了您的问题;但试一试:

$ds = $DataSet.Tables[0]
$ds | Format-Table -Autosize

...

foreach ($row in $ds.Rows) {
    if ($row.setting -eq "ArchiveDir" -and $row.value -like "\\*") {
        $path = $row.value
        Write-Host $path
        Set-Location $path
    }
}

如果您遇到同样的错误,请尝试:

$path = $row.value.ToString()
Set-Location $Path