我需要为 Java 设置一个系统环境变量。我目前正在使用这种方法:
setx /M JAVA_HOME "C:\Program Files (x86)\Java\jdk1.6.0_17"
Run Code Online (Sandbox Code Playgroud)
我的问题:有没有更好的(PowerShell)方法来做到这一点?
我尝试使用:
[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files (x86)\Java\jdk1.6.0_17")
Run Code Online (Sandbox Code Playgroud)
但这不会产生与setx.
有没有更好的 PowerShell 方法来设置 Java 主页?
验证字符串参数的长度时:
[Parameter(Mandatory=$false)]
[ValidateLength(6,128)]
[string]$value
Run Code Online (Sandbox Code Playgroud)
是否有可能不强制执行最大长度(仅限最小值)?我试过了:
[ValidateLength(6,0)]
Run Code Online (Sandbox Code Playgroud)
但这会产生运行时错误.
我正在尝试按属性名称过滤结果。我无法在脚本中使用管道。
Get-CimInstance -ClassName Win32_Product -Property Name -Filter "Microsoft*"
Run Code Online (Sandbox Code Playgroud)
返回错误:Get-CimInstance : Invalid query。
我试图获得类似于此命令的输出:
Get-CimInstance -ClassName Win32_Product | ? {$_.Name -like 'Microsoft*'}
Run Code Online (Sandbox Code Playgroud)
但没有管道到Where-Object.
我究竟做错了什么?
考虑这个简单的脚本:
#!/bin/bash
DIR="$1"
for f in "$DIR"; do
if [[ "$f" == "*.txt" ]];
then
echo "Filename is $f"
fi
done
Run Code Online (Sandbox Code Playgroud)
我只想返回扩展名为.txt的文件.用以下方法调用脚本:
./script1 /home/admin/Documents
Run Code Online (Sandbox Code Playgroud)
什么都不返回 没有错误,只是空白.怎么了?
我正在尝试使用splatting安装.MSI:
$InstallerArgs @{
"DATABASENAME" = "my_database";
"LOCALIP" = "127.0.0.1";
"USERNAME" = "username1";
"/i" = "C:\Files\Installer.msi";
}
Run Code Online (Sandbox Code Playgroud)
然后我打电话 Start-Process:
Start-Process -FilePath msiexec.exe -ArgumentList @InstallerArgs -Wait
Run Code Online (Sandbox Code Playgroud)
这会返回错误: Missing an argument for parameter 'ArgumentList'. Specify a parameter of type 'System.String[]' and try again.
是不是可以使用splatting Start-Process?
我正在尝试Test-Path在注册表中使用示例代码:
$RegistryLocation = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
Run Code Online (Sandbox Code Playgroud)
这很好用:
Test-Path -Path $RegistryLocation
Run Code Online (Sandbox Code Playgroud)
真正.现在没有最后的星号字符:
$NewRegistryLocation = $RegistryLocation.Split("*")
Test-Path -Path $NewRegistryLocation
Run Code Online (Sandbox Code Playgroud)
Cannot bind argument to parameter 'Path' because it is an empty string.
但这有效($NewRegistryLocation变量的值):
Test-Path -Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?