通过PowerShell读取环境变量

时间:2018-04-18 12:21:50

标签: powershell environment-variables

我想读一个名为" [TF] MYVAR"的环境变量。通过PowerShell。但是,以下语法不起作用:

$env:[TF]MYVAR

它会产生以下错误:

  

数组索引表达式缺失或无效。

同时添加引号( - >" $ env:[TF] MYVAR")也无济于事。

1 个答案:

答案 0 :(得分:1)

方括号使命令行解析器混乱。尝试替代变量语法:

const struct {
    CGFloat radius;
    CGFloat xCoord;
    CGFloat yCoord;
    CGFloat startAngle;
    CGFloat endAngle;
    int dir;
} __attribute__((packed)) * entry;
// `const` as the memory `entry` will point to will be read-only.
// `* entry` means entry is a pointer to memory of a struct
// that looks as described above. __attribute__((packed)) means
// the memory must be laid out exactly as shown above and have no
// padding for better alignment of fields.

NSData * data = [fHandle readDataOfLength:sizeof(*entry)];
// `sizeof(*entry)` means the size of the memory entry points to,
// contrary to `sizeof(entry)` which would be the size of entry itself
// and that would simply be the size of a pointer on your system, 8 bytes,
// whereas `sizeof(*entry)` will be 44 bytes.

entry = (const void *)dataBytes;
// Any pointer type can be cased to `void *` and assigning
// a `void *` pointer so some pointer is always allowed by the compiler.

self.radius = (float)entry->radius;
self.centerPos = ccp((float)entry->xCoord, (float)entry->yCoord);
self.startAngle = (float)entry->startAngle;
self.endAngle = (float)entry->endAngle;
self.dir = entry->dir;

另见${<variable name>}

正如下面的@PetSerAl所说,由于命令行解析器处理转义方括号字符的方式,你需要这样做:

Get-Help about_Variables

您还可以使用the native .Net methods,无需转义即可使用

${env:``[TF``]MYVAR}