先是一点背景.
我的任务是使用GPG(gnupg.org)使用Powershell脚本加密文件.我正在调用的特定exe只是gpg.exe.我想在执行命令时捕获输出.
例如,我在powershell中导入一个公钥,如下所示:
& $gpgLocation --import "key.txt"
Run Code Online (Sandbox Code Playgroud)
$ gpgLocation只是gpg.exe的文件位置(默认为"C:\ Program Files\GNU\GnuPG\gpg.exe"
我的全部问题是,如果我尝试:
& $gpgLocation --import "key.txt" | out-file gpgout.txt
Run Code Online (Sandbox Code Playgroud)
我得到的只是一个1kb文件,名称相应,但它完全是空白的.我已经为out-file尝试了几个标志,看看我是否遇到了怪癖.
我也尝试将命令发送到此代码(并使用通常的out-file等捕获输出):
param
(
[string] $processname,
[string] $arguments
)
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo;
$processStartInfo.FileName = $processname;
$processStartInfo.WorkingDirectory = (Get-Location).Path;
if($arguments) { $processStartInfo.Arguments = $arguments }
$processStartInfo.UseShellExecute = $false;
$processStartInfo.RedirectStandardOutput = $true;
$process = [System.Diagnostics.Process]::Start($processStartInfo);
$process.WaitForExit();
$process.StandardOutput.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我很绝望!
嗨同伴pythonistas,在PowerShell中使用virtualenv时似乎有问题.
当我尝试在PowerShell中激活我的环境时...
> env/scripts/activate
.. 什么都没发生.(shell提示符应该更改以及PATH环境变量.)
我想问题是PowerShell产生了一个新的cmd.进程只是为了运行activate.bat,从而在完成后将activate.bat更改为shell死.
你有什么问题的解决方法吗?(我现在坚持使用cmd.exe)
使用BAT/CMD脚本我可以简单地使用"msiexec /i <whatever.msi> /quiet /norestart"然后检查%errorlevel%结果.
与VBScript使用Wscript.Shell对象Run()的方法,我能得到这样的结果:
"result = oShell.Run("msiexec /i ...", 1, True)"
Run Code Online (Sandbox Code Playgroud)
如何使用PowerShell执行此操作?
使用powershell,您可以使用'&'字符运行另一个应用程序并传入参数.
一个简单的例子.
$notepad = 'notepad'
$fileName = 'HelloWorld.txt'
# This will open HelloWorld.txt
& $notepad $fileName
Run Code Online (Sandbox Code Playgroud)
这很好.但是,如果我想使用业务逻辑来动态生成命令字符串呢?使用相同的简单示例:
$commandString = @('notepad', 'HelloWorld.txt') -join ' ';
& $commandString
Run Code Online (Sandbox Code Playgroud)
我收到错误:
术语"记事本HelloWorld.txt"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
在我的实例中,我试图动态添加或删除最终命令行字符串的选项.我有办法解决这个问题吗?
我对这似乎是一个简单的问题感到难过; 很抱歉这里有任何骨头的问题.
我有脚本清理已解散的备份文件.在识别出我循环的文件并打印出被转储的内容之后.我的问题出现了尝试在零失效文件时提供反馈/测试.脚本看起来像......
$Files = Get-ChildItem $BackupPath_Root -include *.bak -recurse
| where {$_.CreationTime -le $DelDate_Backup }
if ( $Files -eq "" -or $Files.Count -eq 0 ) {
write-host " no files to delete." #<-- this doesn't print when no files
} else {
foreach ($File in $Files) {
write-host “$File”
Remove-Item $File | out-null
}
}
Run Code Online (Sandbox Code Playgroud)
if检查没有文件没有捕获无文件条件.测试$Files没有结果的适当方法是什么?
我想在我的代码中使用Get-Credential cmdlet.
如何从System.Security.SecureString格式轻松解码密码?
(我必须在代码的一个部分以明文格式使用密码)
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString
$credential
#How to show password in text format?
Run Code Online (Sandbox Code Playgroud)
我的解决方法,但我认为还有一个正常的方法
$credCachePS = New-Object System.Net.CredentialCache
$credCachePS.Add("uridummy", "NTLM", $credential)
$credCachePS | select Password
Run Code Online (Sandbox Code Playgroud) 我正在编写一个PowerShell脚本,它将使用Invoke-Command及其-ScriptBlock参数在远程主机上执行命令.例如,
function Foo {
...
return "foo"
}
$rv = Invoke-Command --Credential $c --ComputerName $fqdn -ScriptBlock ${function:Foo}
Run Code Online (Sandbox Code Playgroud)
这很好用.我现在要做的是同样的事情,但用本地参数调用函数.例如,
function Bar {
param( [String] $a, [Int] $b )
...
return "foo"
}
[String] $x = "abc"
[Int] $y = 123
$rv = Invoke-Command --Credential $c --ComputerName $fqdn -ScriptBlock ${function:Foo($x,$y)}
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
Invoke-Command:无法验证参数'ScriptBlock'的参数.参数为null.提供非null参数并再次尝试该命令.
如何使用-ScriptBlock带有参数的本地函数的Invoke-Command ?
我意识到我可以将整个函数和参数包装在一个大的代码块中,但在我看来,这并不是一种干净的方式.
PowerShell的Set-ExecutionPolicy命令被禁止,所以我不能这样运行:
PS> .\script.ps1 (enter)
Run Code Online (Sandbox Code Playgroud)
除了"Windows PowerShell ISE"之外,还有其他方法可以运行PowerShell脚本吗?
PS:我能够使用Java的ProccessBuilder来运行单个PowerShell命令,但不知道如何运行整个脚本.
我试图检查端口是否打开使用PowerShell,如下所示.
(new-object Net.Sockets.TcpClient).Connect("10.45.23.109", 443)
Run Code Online (Sandbox Code Playgroud)
此方法有效,但输出不是用户友好的.这意味着如果没有错误,那么它就具有访问权限.有没有办法检查成功并显示一些消息,如"端口443是否可操作"?
我试图从XML标记访问和更改特定属性
XML:
<office>
<staff branch="Hanover" Type="sales">
<employee>
<Name>Tobias Weltner</Name>
<function>management</function>
<age>39</age>
</employee>
<employee>
<Name>Cofi Heidecke</Name>
<function>security</function>
<age>4</age>
</employee>
</staff>
<staff branch="London" Type="Technology">
<employee>
<Name>XXXX</Name>
<function>gement</function>
<age>39</age>
Run Code Online (Sandbox Code Playgroud)
从上面的例子我想打印分支属性,然后想要在所有整个XML中使用一个值(如纽约)更改它,并使用下面的代码来执行该操作
$xml=New-Object XML
$xml.Load("C:\FE6Work.xml")
$node=$xml.SelectNodes("/office/staff")
write-output $node.branch
$node.branch="New York"
Run Code Online (Sandbox Code Playgroud)
但得到一个错误说明无法找到该元素.
有人可以帮忙吗?
powershell ×10
credentials ×1
exit-code ×1
java ×1
msiexec ×1
python ×1
return-value ×1
virtualenv ×1
xml ×1