Looping through a hash, or using an array in PowerShell

后端 未结 7 1651
暗喜
暗喜 2020-12-22 21:13

I\'m using this (simplified) chunk of code to extract a set of tables from SQL Server with BCP.

$OutputDirectory = \"c:\\junk\\\"
$ServerOption =   \"-SServe         


        
7条回答
  •  孤城傲影
    2020-12-22 21:37

    If you're using PowerShell v3, you can use JSON instead of a hashtable, and convert it to an object with Convert-FromJson:

    @'
    [
        {
            FileName = "Page";
            ObjectName = "vExtractPage";
        },
        {
            ObjectName = "ChecklistItemCategory";
        },
        {
            ObjectName = "ChecklistItem";
        },
    ]
    '@ | 
        Convert-FromJson |
        ForEach-Object {
            $InputFullTableName = '{0}{1}' -f $TargetDatabase,$_.ObjectName
    
            # In strict mode, you can't reference a property that doesn't exist, 
            #so check if it has an explicit filename firest.
            $outputFileName = $_.ObjectName
            if( $_ | Get-Member FileName )
            {
                $outputFileName = $_.FileName
            }
            $OutputFullFileName = Join-Path $OutputDirectory $outputFileName
    
            bcp $InputFullTableName out $OutputFullFileName -T -c $ServerOption
        }
    

提交回复
热议问题