Powershell 强类型环境变量
Powershell strongly typed environment variable
我想设置一个强类型环境变量,当我使用 $env:
时它似乎 "lose" 它的类型
在原来的函数中它工作正常:
function Create-ThisCrap(){
[Microsoft.ApplicationInsights.TelemetryClient] $mytest = New-Object -TypeName Microsoft.ApplicationInsights.TelemetryClient
$mytest.InstrumentationKey = "213-123"
$mytest.TrackTrace("something")
}
但这已经不是了:
function Create-ThisCrap(){
[Microsoft.ApplicationInsights.TelemetryClient] $env:mytest = New-Object -TypeName Microsoft.ApplicationInsights.TelemetryClient
$env:mytest.InstrumentationKey = "213-123"
$env:mytest.TrackTrace("something")
}
有错误:
无法在此对象上找到 属性 'InstrumentationKey'。
方法调用失败,因为 [System.String] 不包含名为 'TrackTrace'.
的方法
我试过作弊:
function Create-ThisCrap(){
[Microsoft.ApplicationInsights.TelemetryClient] $mytest = New-Object -TypeName Microsoft.ApplicationInsights.TelemetryClient
$mytest.InstrumentationKey = "213-123"
$mytest.TrackTrace("something")
return $mytest
}
function FROMAnotherScript(){
[Microsoft.ApplicationInsights.TelemetryClient] $env:HopeItWorks = Create-ThisCrap
$env:HopeItWorks.TrackTrace('whatever')
}
但它产生了相同的效果。
我大致知道 Powershell 传递 类 时的问题(显然还不够),但我认为只要它在一个函数内它就应该可以工作并且$env 问题难倒了我。
环境变量只能是字符串。如果需要存储多个值,可以使用多个环境变量,但数据类型始终为字符串。环境变量源自 MS-DOS,这意味着它们来自 1980 年代,旨在与批处理文件和 DOS 命令一起使用。它们是非常古老的遗留组件。
此外,通过$env:
命名空间设置环境变量不会为其他进程永久设置环境变量。您可以通过调用 [System.Environment]::SetEnvironmentVariable()
来做到这一点,但即便如此,设置环境变量也只会为设置值后创建的进程设置值。它不会刷新其他线程已经 运行.
您不能将一个对象从一个进程完整地传递到另一个进程。您需要以某种方式对其进行序列化,然后在另一端对其进行反序列化。通常这是通过将您的设置存储在数据文件、数据库或注册表中来实现的。您可以尝试 Export-Clixml
和 Import-Clixml
,它们为 PowerShell 提供基本的数据类型感知对象序列化,但我无法真正判断您要做什么。
我想设置一个强类型环境变量,当我使用 $env:
时它似乎 "lose" 它的类型在原来的函数中它工作正常:
function Create-ThisCrap(){
[Microsoft.ApplicationInsights.TelemetryClient] $mytest = New-Object -TypeName Microsoft.ApplicationInsights.TelemetryClient
$mytest.InstrumentationKey = "213-123"
$mytest.TrackTrace("something")
}
但这已经不是了:
function Create-ThisCrap(){
[Microsoft.ApplicationInsights.TelemetryClient] $env:mytest = New-Object -TypeName Microsoft.ApplicationInsights.TelemetryClient
$env:mytest.InstrumentationKey = "213-123"
$env:mytest.TrackTrace("something")
}
有错误:
无法在此对象上找到 属性 'InstrumentationKey'。
方法调用失败,因为 [System.String] 不包含名为 'TrackTrace'.
的方法
我试过作弊:
function Create-ThisCrap(){
[Microsoft.ApplicationInsights.TelemetryClient] $mytest = New-Object -TypeName Microsoft.ApplicationInsights.TelemetryClient
$mytest.InstrumentationKey = "213-123"
$mytest.TrackTrace("something")
return $mytest
}
function FROMAnotherScript(){
[Microsoft.ApplicationInsights.TelemetryClient] $env:HopeItWorks = Create-ThisCrap
$env:HopeItWorks.TrackTrace('whatever')
}
但它产生了相同的效果。
我大致知道 Powershell 传递 类 时的问题(显然还不够),但我认为只要它在一个函数内它就应该可以工作并且$env 问题难倒了我。
环境变量只能是字符串。如果需要存储多个值,可以使用多个环境变量,但数据类型始终为字符串。环境变量源自 MS-DOS,这意味着它们来自 1980 年代,旨在与批处理文件和 DOS 命令一起使用。它们是非常古老的遗留组件。
此外,通过$env:
命名空间设置环境变量不会为其他进程永久设置环境变量。您可以通过调用 [System.Environment]::SetEnvironmentVariable()
来做到这一点,但即便如此,设置环境变量也只会为设置值后创建的进程设置值。它不会刷新其他线程已经 运行.
您不能将一个对象从一个进程完整地传递到另一个进程。您需要以某种方式对其进行序列化,然后在另一端对其进行反序列化。通常这是通过将您的设置存储在数据文件、数据库或注册表中来实现的。您可以尝试 Export-Clixml
和 Import-Clixml
,它们为 PowerShell 提供基本的数据类型感知对象序列化,但我无法真正判断您要做什么。