最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - I'm trying to print a value for a json key that has brackets double qutoes and commas - Stack Overflow

programmeradmin1浏览0评论

PowerShell 5.1

Line snippet of json file that's of interest:

"MyAddresses": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"],

is there a way to print that value as is? the following prints out the value but without brackets, double quotes, and commas which is ok

# Define the path to the JSON file on the remote computers
$jsonFilePath = "C:\mydata.json"

    # Script block to run on each remote computer
    $scriptBlock = {
        param ($jsonFilePath)
        
        try {
            # Read the JSON file content
            $jsonContent = Get-Content -Path $jsonFilePath -Raw | ConvertFrom-Json
    
            # Get the value of the ReceiverAddress key
            $receiverAddress = $jsonContent.Notification.ReceiverAddresses
    
            # Output the value
            Write-Output "******************************** $($env:COMPUTERNAME) - START OF DATA ********************************************"
            Write-Output "ReceiverAddress: $receiverAddress"
            Write-Output "******************************* $($env:COMPUTERNAME) - END OF DATA *********************************************"
        }
        catch {
            Write-Output "Error on $($env:COMPUTERNAME): $_"
        }
    }
    
    # Define the list of remote computers
    $computers = 
    @"
    server1
    server2
    "@ -split [Environment]::NewLine
    
    # Run the script block on each remote computer in parallel
    $jobs = Invoke-Command -ComputerName $computers -ScriptBlock $scriptBlock -ArgumentList $jsonFilePath -AsJob
    
    # Wait for all jobs to complete
    $jobs | ForEach-Object { $_ | Wait-Job }
    
    # Retrieve and display the results
    $jobs | ForEach-Object {
        try {
            $result = Receive-Job -Job $_
            Write-Output $result
        }
        catch {
            Write-Output "Failed to retrieve job results for $($_.Location): $_"
        }
        finally {
            Remove-Job -Job $_
        }
    }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论