小编Guy*_*mas的帖子

PowerShell,使用没有值的参数调用脚本,就像带有选项的脚本一样

是否可以使用选项调用PowerShell脚本?就像没有值的参数一样.

例如,我目前使用:

param (
    $lastmonth=0
);
Run Code Online (Sandbox Code Playgroud)

在脚本中.现在我可以使用这个电话了

script.ps1 -lastmonth 1
Run Code Online (Sandbox Code Playgroud)

要么

script.ps1 
Run Code Online (Sandbox Code Playgroud)

并使用$ lastmonth来控制流量.

但我想接听电话

script.ps1 -lastmonth
Run Code Online (Sandbox Code Playgroud)

和

script.ps1
Run Code Online (Sandbox Code Playgroud)

并根据是否给出-lastmonth来控制流量.

powershell

18
推荐指数
1
解决办法
1万
查看次数

如何让PowerShell 4 cmdlet(如Test-NetConnection)在Windows 7上运行?

情况.在Windows 7 SP1计算机上,我已使用Windows6.1-KB2819745-x64-MultiPkg.msu进行了更新.此外,在PowerShell $ PSVersionTable现在报告'PSVersion 4.0'.

目前,我的结论是许多PowerShell 4 cmdlet如Test-NetConnection,只能在Windows 8.1上运行.但是,我想知道是否有可以在我的Windows 7机器上导入PowerShell 4模块的解决方法.

powershell windows-7 powershell-4.0

17
推荐指数
3
解决办法
5万
查看次数

PowerShell参数值列表

我一直在努力寻找答案,但我认为在一组关键字中将PowerShell和参数放在一起并不能轻松搜索

我的问题是,我正在编写一个函数,并且我提供了一个参数,但参数必须是特定字符串列表中的一个.

有没有办法在脚本中提供这些字符串,这样如果我输入"myfunction -parameter ..",我可以使用tab完成参数值吗?

parameters powershell

17
推荐指数
1
解决办法
3万
查看次数

Powershell替换存储在许多文件夹中的多个文件中的文本

我想替换多个文件和文件夹中的文本.文件夹名称更改,但文件名始终为config.xml.

$fileName = Get-ChildItem "C:\config\app*\config.xml" -Recurse
(Get-Content $fileName) -replace 'this', 'that' | Set-Content $fileName
Run Code Online (Sandbox Code Playgroud)

当我运行上面的脚本时,它可以工作,但它在config.xml中写入整个文本大约20次.怎么了?

directory powershell replace file

8
推荐指数
3
解决办法
2万
查看次数

PowerShell启动作业增量

我很好奇为什么Start-Job增加了两个.我担心的是我做错了什么使得新工作的ID跳了2.

Start-Job -ScriptBlock {Get-WinEvent -LogName system -MaxEvents 1000} 
Run Code Online (Sandbox Code Playgroud)

结果如Get-Job所示

Id Name   State     HasMoreData Command                                                                                                                                                                     
-- ----   -----     ----------- -------                                                                                                                                                                     
 2 Job2   Completed       False Get-WinEvent -LogName system -MaxEvents 1000                                                                                                                                
 4 Job4   Completed       False Get-WinEvent -LogName system -MaxEvents 1000                                                                                                                                
 6 Job6   Completed        True Get-WinEvent -LogName system -MaxEvents 1000  
Run Code Online (Sandbox Code Playgroud)

问题:您可以控制Start-Job Id增量,还是强制它们只是1?

powershell powershell-2.0 powershell-3.0

7
推荐指数
2
解决办法
1015
查看次数

PowerShell:在input type = file Form中写入值.

我在网页上有一个输入类型=文本输入框我加载并填充值并单击提交按钮,工作正常:

$ie=New-Object -comobject InternetExplorer.Application 
$ie.visible=$true 
$ie.Navigate("https://myurl/test.html") 
while($ie.busy){Start-Sleep 1} 
$ie.Document.getElementById("field_firstName").value="Firstname" 
$ie.Document.getElementById("field_lastName").value="Lastname" 
$ie.Document.getElementById("btn_upload").Click() 
while($ie.busy){Start-Sleep 1}
Run Code Online (Sandbox Code Playgroud)

我还想用c:\ temp\test.txt 填充输入类型=文件框并上传它.我读到,由于安全原因,浏览器不支持value =.

使用PowerShell有没有解决方法?也许"点击"浏览按钮并选择文件或使用sendkeys?

html powershell internet-explorer file-upload input

5
推荐指数
1
解决办法
9821
查看次数

用于重新启动服务的 PowerShell 脚本

我的任务是按键盘序列,如Ctrl+ Shift+ R,重新启动Windows服务。

我有一个在 PowerShell ISE 中运行良好的脚本,当以管理权限启动时。

当我尝试使用 PowerShell 脚本时,由于管理权限不足而失败。我可以让它与老式的 bat 文件一起工作,但不能与 PowerShell 一起工作,这真是令人恼火。

问题的根源在于 PowerShell 脚本的快捷方式的管理权限框变灰。到目前为止,还没有解决方法可以克服这个特权问题。

有任何想法吗?

powershell windows-services keyboard-shortcuts shortcut

5
推荐指数
1
解决办法
3255
查看次数

Get-ChildItem-筛选器数组

情况:

  1. Get-ChildItem $Path -Filter *.dll 为我工作

  2. 这有效:

    $Path = "$env:windir\system32\*"
    $GuyArray = @("*.dll", "*.exe")
    
    Get-ChildItem $Path -Include $GuyArray
    
    Run Code Online (Sandbox Code Playgroud)
  3. 但是我无法使它工作:

    $Path = "$env:windir\system32\*"
    $GuyArray = @("*.dll", "*.exe")
    
    Get-ChildItem $Path -Filter $GuyArray
    
    Run Code Online (Sandbox Code Playgroud)

错误信息:

无法将“ System.Object []”转换为参数“ Filter”所需的类型“ System.String”。不支持指定的方法。

问题:

  1. 这是否意味着-Include支持多个值,但-Filter仅允许一个值?
  2. 如果以上解释正确,有没有办法我可以从中发现Get-Help gci呢?

powershell filter

5
推荐指数
1
解决办法
5560
查看次数

安装v4后,您能模仿PowerShell v3吗?

我想模仿PowerShell 3.0

在PowerShell v3或v4中,您可以使用以下命令模拟v2:

%windir%\system32\WindowsPowerShell\v1.0\PowerShell.exe -version 2
Run Code Online (Sandbox Code Playgroud)

$ Host揭示:版本:2.0

但是,在PowerShell 4中

%windir%\system32\WindowsPowerShell\v1.0\PowerShell.exe -version 3
Run Code Online (Sandbox Code Playgroud)

$ Host now报告:版本:4.0(我希望3:0)

问题:有没有办法从4.0模拟PowerShell 3.0?

powershell powershell-3.0 powershell-4.0

4
推荐指数
1
解决办法
1023
查看次数

以管理员身份运行 - 注册表设置

PowerShell(和其他程序)提供"以管理员身份运行"的功能.是否有注册表我们可以更改设置以使其成为默认设置?因此,无需右键单击并以"以管理员身份运行"?

registry powershell

3
推荐指数
1
解决办法
2万
查看次数

PowerShell打开Outlook,让您可见

我的任务是从使用PowerShell开始创建一个Outlook实例,只需查看或显示我在TaskManager中可以看到的进程.

对于Digress,这适用于Word.Application
但不适用于Outlook.Application.

$MsApp = New-Object -comObject Word.Application 

$MsApp.Visible = $true
Run Code Online (Sandbox Code Playgroud)

我已经检查了方法,但找不到合适的动词来打开,运行或显示.

我会非常感谢解决方案.

powershell outlook

2
推荐指数
1
解决办法
2万
查看次数

如何自动运行提示UI选择的PowerShell脚本?

我有一个看起来像这样的powershell脚本p1.ps1(不完全是,但这可以作为一个例子):

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Do the action."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Exit."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
Run Code Online (Sandbox Code Playgroud)

如何编写另一个脚本p2.ps1,它将运行第一个脚本(p1.ps1)并为其提供答案,以便p2.ps1执行时不会问任何问题?

我试过了:

echo y | p1.ps1
Run Code Online (Sandbox Code Playgroud)

但它没有做到这一点.

powershell scripting

1
推荐指数
1
解决办法
2185
查看次数