我需要在运行时在PowerShell脚本中显示所有已配置的环境变量.通常在显示环境变量时,我可以在shell中使用以下其中一种(除了其他技术,但这些很简单):
gci env:*
ls Env:
Run Code Online (Sandbox Code Playgroud)
但是,我从另一个程序调用了一个脚本,当我在脚本中使用上述调用之一时,而不是显示环境变量及其值,而是获取System.Collections.DictionaryEntry类型列表而不是变量及其值.在PowerShell脚本中,如何显示所有环境变量?
我总是使用以下语法来确保变量在字符串中展开:
"my string with a $($variable)"
我最近遇到了以下语法:
"my string with a ${variable}"
它们是等价的吗?有什么区别吗?
如何开始我刚定义的函数的作业?
function FOO { write-host "HEY" } Start-Job -ScriptBlock { FOO } |
Receive-Job
Receive-Job: The term 'FOO' is not recognized as the name of cmdlet,
function ,script file or operable program.
Run Code Online (Sandbox Code Playgroud)
我该怎么办?谢谢.
我找不到传递函数的方法。只是变数。
没有将函数放在 ForEach 循环中的任何想法?
function CustomFunction {
Param (
$A
)
Write-Host $A
}
$List = "Apple", "Banana", "Grape"
$List | ForEach-Object -Parallel {
Write-Host $using:CustomFunction $_
}
Run Code Online (Sandbox Code Playgroud)
我正在编写一个 Powershell 脚本,它将集成到为 32 位 Windows 计算机设计的产品中。因此,在调用时,它默认会在 x86 Powershell 上运行,甚至在 64 位计算机上也是如此。我尝试使用[System.IntPtr]::Size,但输出与同一台计算机上的 Powershell 版本不同。
Powershell(32 位)-
PS D:\powershellScripts> [System.IntPtr]::Size
4
Run Code Online (Sandbox Code Playgroud)
在同一台机器上的 Powershell(64 位)-
PS D:\powershellScripts> [System.IntPtr]::Size
8
Run Code Online (Sandbox Code Playgroud)
我需要一个独立的解决方案来帮助我区分底层机器的地址大小。
简而言之,有没有办法从 shell 确定PSProvider 的路径是否有伴随变量,如果有,该变量的名称是什么?此处列出了支持此表示法的内置 PowerShell 提供程序,但我指的是一般提供程序 - 内置/第三方/自定义等。提供程序的驱动器名称(可以通过Get-PSProvider) 总是与其变量符号名称相同?
例如:
环境提供程序使用Env:也可以使用其变量访问的驱动器$env:
证书提供程序使用的Cert:驱动器也可以使用其变量进行访问$cert:,但没有实现IContentCmdletProvider 接口 - 如引用路径时在 shell 中触发的以下错误消息所示$Cert:xyz(但我可以使用CTRL+Space和tab自动完成$cert:路径)
提供程序“证书”不能用于使用变量语法获取或设置数据。无法使用接口。该提供程序未实现 IContentCmdletProvider 接口。
这(下面引用)是否意味着每个提供程序的驱动器自动使用变量符号(并且始终具有相应的变量),但并非所有提供程序驱动器都能够通过提供程序的变量语法公开/操作其内容/项目?
“您可以使用美元 ($) 符号为提供程序路径添加前缀,并访问实现 IContentCmdletProvider 接口的任何提供程序的内容。”
最后,以下说法是否准确?
询问“是否有办法从 shell 确定 PSProvider 是否实现了IContentCmdletProvider接口?” 可能不足以确定是否可以使用其变量符号来访问/操作提供者的驱动器内容。
Function Check-PC
{
$PC = Read-Host "PC Name"
If($PC -eq "exit"){EXIT}
Else{
Write-Host "Pinging $PC to confirm status..."
PING -n 1 $PC
}
Run Code Online (Sandbox Code Playgroud)
这是我写到 PowerShell 脚本中的一个函数片段。我希望该函数在 PowerShell 的新实例中运行,而不是在主窗口中运行。
是否可以在 PowerShell 的单独进程中运行它而不将其编写为单独的脚本并调用脚本?像这样的东西:
$x= Start-Process powershell | Check-PC
Run Code Online (Sandbox Code Playgroud)
如果可能,我需要将所有内容都保存在一个脚本中。
未指定PSDrive时,以下工作:
${[foo]}="bar"
echo ${[foo]}
Run Code Online (Sandbox Code Playgroud)
但是以下方法不起作用
$env:${[foo]}="bar"
At line:1 char:1
+ $env:${[foo]}="bar"
+ ~~~~~
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.
At line:1 char:6
+ $env:${[foo]}="bar"
+ ~~~~~~~~~~~~~~
Unexpected token '${[foo]}="bar"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidVariableReferenceWithDrive
Run Code Online (Sandbox Code Playgroud)
${env:[foo]}="bar"
Cannot find path 'env:[foo]' because it does not exist.
At line:1 char:1
+ ${env:[foo]}="bar"
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : …Run Code Online (Sandbox Code Playgroud) 我试图做的是让雷达在将它移动到人文件夹后删除从 web 客户端请求的电影,所以如果默认路径是 D:\Movies\ 那么只需记录它,如果它转到 D 以外的任何其他地方:\Movies\ 然后它将从客户端中删除它。
寻找一些指导,因为我对 powershell 完全陌生。
$movie_path = $env:radarr_moviefile_relativepath
$default_path = "D:\Movies\"
$RADARRIP="localhost"
$RADARRPORT="7878"
$RADARRAPIKEY="******"
$Logfile = "C:\Custom Scripts\Radarr.log"
Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}
if ($default_path == $movie_path)
{
LogWrite $movie_path $radarr_movie_id "added to server"
LogWrite " "
}
else
{
Invoke-WebRequest -Uri http://$RADARRIP:$RADARRPORT/api/movie/$radarr_movie_id" -X GET -H "X-Api-Key: $RADARRAPIKEY -k
LogWrite $movie_path $radarr_movie_id "added to server"
LogWrite $radarr_movie_id "selected movie from server"
Invoke-WebRequest -Uri http://$RADARRIP:$RADARRPORT/api/movie/$radarr_movie_id" -X DELETE -H "X-Api-Key: $RADARRAPIKEY …Run Code Online (Sandbox Code Playgroud) 我有一个很长的剧本。我有一个记录功能:
function Log ([string]$Content){
$Date = Get-Date
Add-Content -Path $LogPath -Value ("$Date : $Content")
}
Run Code Online (Sandbox Code Playgroud)
在脚本的某些时候,我需要并行运行作业。我有一个计算机名称列表,我需要对每个计算机名称使用 psexec。这应该作为并行运行的作业来完成
Start-Job -ScriptBlock {
Log "$line Has called"
$Program_List = gc $USERS_DB_PATH\$line.txt | select -Skip 1
if (Test-Connection $line -Quiet) {
ForEach ($program in $Program_List){
Log "$line $program"
#"line $line bot is $bot pwd is $pwd"
psexec \\"$line" -u bla.local\"$bot" -p $pwd cmd bla
}
}
else{
Log "Cannot Connect to $line"
}
}
#Remove-Item "$USERS_DB_PATH\$line.txt"
}
Run Code Online (Sandbox Code Playgroud)
我知道这与范围有关,但是如何使这个脚本块看到函数 Log 和所有必要的变量?他们都空了
我正在编写一个运行几个后台作业的 PowerShell 脚本。其中一些后台作业将使用相同的一组常量或实用函数,如下所示:
$FirstConstant = "Not changing"
$SecondConstant = "Also not changing"
function Do-TheThing($thing)
{
# Stuff
}
$FirstJob = Start-Job -ScriptBlock {
Do-TheThing $using:FirstConstant
}
$SecondJob = Start-Job -ScriptBlock {
Do-TheThing $using:FirstConstant
Do-TheThing $using:SecondConstant
}
Run Code Online (Sandbox Code Playgroud)
如果我想在子作用域中共享变量(或者在本例中为常量),我会在变量引用前面加上$using:. 但我不能用函数来做到这一点;按原样运行此代码会返回错误:
The term 'Do-TheThing' is not recognized as the name of a cmdlet, function, script file, or operable program.
Run Code Online (Sandbox Code Playgroud)
我的问题是:我的后台作业如何使用我在更高范围内定义的小型实用函数?
powershell ×11
scope ×2
syntax ×2
function ×1
jobs ×1
namespaces ×1
scriptblock ×1
start-job ×1
windows ×1