json对象添加字段

图表(table)型数据、输入输出文件

https://blog.netwrix.com/2018/10/04/powershell-variables-and-arrays/

$var1 = “Hello World”
$var1 | Out-File HelloWorld.txt # 当前目录输出

$var2 = Get-Process
$var2 | SELECT Name, Path | Export-Csv -Path D:\scripts\processes.csv #-Path可以省略。路径必须具有可写权限,否则会失败

$var2 | ConvertTo-HTML -Property Name, Path > D:\scripts\processes.html

Get-Content c:\scripts\processes.csv # 读取文件内容,返回行数组。加-Raw参数返回整个文件内容字符串

(Get-ChildItem -Filter *.uproject) | Select-Object -First 1 | Get-Content -Raw | ConvertFrom-Json # 读取文件内容并构造一个ps里的json对象

创建变量及赋值 → ...

https://blog.netwrix.com/2018/10/04/powershell-variables-and-arrays/

$var_str1 = "Hello";
$var_str2 = "World";
$var_str3 = $var_str1 + $var_str2
$var_str3  # HelloWorld

$var_int1 = 10;
$var_int2 = 20;
$var_int3 = $var_int1 + $var_int2
$var_int3  # 30

$var_type_conv = $var_str1 + $var_int1
$var_type_conv  # Hello10

$str_to_int = [int]"123"

$var_array = "apple", "orange", 123
$var_array.GetType().FullName # 属性和命令均不区分大小写
$var_array2 = @(elem1, elem2)

$var_hashtable = @{
    "key1" = "value1"; # 能用分号或者直接留空,但不能加逗号
    "key2" = 123
}

# 作用域
# Global: 在当前powershell交互环境(会话)里一直有效
# Script: 整个psl脚本
# Local: 当前作用域(块)

$global:var
$script:var
$local:var

# 清空变量,或置空
Clear-Variable -Name var_str1 # 现在var_str1值为$null

# 销毁变量
Remove-Variable $var_str1 # 现在var_str1变量不复存在,访问会报错

Set-Variable -Name "myVariable" -Description "This is my sample variable"



多行字符串

$cwd = (Get-Item .).FullName

$uproject_file = Get-ChildItem . -Filter "*.uproject" | Select-Object -First 1
if ($null -eq $uproject_file) {
    Write-Host "uproject file not found."
    Exit -1
}

$uproject = $uproject_file | Get-Content -Raw | ConvertFrom-Json
$project_name = $uproject_file.BaseName
Write-Host "Project name is $project_name"
$modules = @(
    @{
        "Name" = "$project_name"
        "Type" = "Runtime"
        "LoadingPhase" = "Default"
    }
)
#$modules是个数组,里面的元素是一个字典

if ($null -eq $uproject.Modules) {
    Write-Host "Modules field not exists."
    $uproject | Add-Member -Name "Modules" -Value $modules -MemberType NoteProperty
    Write-Host "Adding [Modules] field:",($uproject.Modules| ConvertTo-Json)
    <# save uproject file #>
    $uproject | ConvertTo-Json -Depth 3 | Out-File $uproject_file.FullName
}
else {
    <# Action when all if and elseif conditions are false #>
    $project_name = $uproject.Modules[0].Name
}