标签: powershell

Powershell脚本运行带有参数的exe文件

我需要脚本来运行带参数的 exe 文件。这就是我写的,如果有更好的方法吗?

$Command = "\\Networkpath\Restart.exe"
$Parms = "/t:21600 /m:360 /r /f"
$Prms = $Parms.Split(" ")
& "$Command" $Prms
Run Code Online (Sandbox Code Playgroud)

谢谢

windows powershell scripting exe

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

Azure 逻辑应用:如何运行 Powershell 脚本或 Azure CLI?

我正在构建我的 Azure 逻辑应用程序工作流,它应该检查一些条件并按照 Powershell 运行:

Stop-AzureWebsiteJob -Name MyWebsite -JobName MyWebJob
Start-AzureWebsiteJob -Name MyWebsite -JobName MyWebJob -JobType Continuous
Run Code Online (Sandbox Code Playgroud)

问题是:在 Azure 逻辑应用程序中调用此类脚本的最简单方法是什么?Powershell 似乎没有内置块/连接器,所以我想知道有哪些可能性。或者,使用类似的操作运行 az CLI 命令可能会更容易

powershell azure azure-cli azure-logic-apps

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

Powershell 不允许我对蓝牙设备使用 disable-pnpdevice

几天前我问了这个问题, 如何在 windows powerhell 中连接和删除蓝牙,但是每当我尝试运行代码时:

$device = Get-PnpDevice | Where-Object {$_.Class -eq "Bluetooth" -and $_.FriendlyName -eq "MX Master 2S"}
Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
Start-Sleep -Seconds 10
Enable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Disable-PnpDevice : Generic failure 
At C:\Users\wadeb\OneDrive\Desktop\Mouse Bluetooth Reset.ps1:2 char:1
+ Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Win32_PnPEntity...B17A1&0&FAD...):ROOT\cimv2\Win32_PnPEntity) [ 
   Disable-PnpDevice], CimException
    + FullyQualifiedErrorId : HRESULT 0x80041001,Disable-PnpDevice
Run Code Online (Sandbox Code Playgroud)

我正在使用提升的权限运行它,并且没有错误代码enable-pnpdevice,只有 disable-pnpdevice 有问题。

有谁知道如何做到这一点?在 paul 发表评论后,我再次检查我是否是管理员,我不是,但是当我这样做时,现在会弹出:

Disable-PnpDevice : 不支持 At line:2 char:1 + Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false …

powershell bluetooth plug-and-play

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

在 PowerShell 中使用枚举类型

我在 powershell 中有点新(预期消息开始:))。我无法在这里或其他地方找到我的问题的答案,所以有人可以帮助我吗?

例如,我们想使用一些在 .NET 库中声明的枚举。我们如何在 .NET 中使用它?就像:

using System.Data.SqlClient;

public class Test {
   var x = SortOrder.Ascending;
}
Run Code Online (Sandbox Code Playgroud)

如果我需要在 powershell 中使用它,据我所知,我需要编写如下内容:

$x = [System.Data.SqlClient.SortOrder]::Ascending
Run Code Online (Sandbox Code Playgroud)

所以,问题是,是否有可能在 powershell 中使用类似 C# 中的“使用”来将语法缩短为类似:$sortOrder.Ascending或[SortOrder]::Ascending?

powershell enums powershell-2.0 powershell-3.0

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

如何在 PowerShell Get-ChildItem 的通配符模式中使用方括号?

我想列出所有以方括号中的文本结尾的文件。

但无论是Get-ChildItem *[*也没有Get-ChildItem *`[*,也没有Get-ChildItem *``[*工作。

我怎样才能毫不费力地完成这项工作(即通过创建变量,通过管道运行其他命令等)

powershell

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

从字符串(不是文件)获取 XML

我有一个 PowerShell 脚本:

$xmlString="<root>
        <section>
            <node id='1'>AAA</node>
            <node id='2'>BBB</node>
            <node id='3'>CCC</node>
        </section>
    </root>"

$xml = New-Object -TypeName System.Xml.XmlDocument
$content = $xml.LoadXml($xmlString)
Run Code Online (Sandbox Code Playgroud)

的$content值为null

$xml变量的内部异常是<Error retrieving property - ArgumentException>

我已经检查过字符串是否以 开头,[System.Text.Encoding]::UTF8.GetPreamble()但没有。

您能告诉我将此类字符串转换为 XML 的正确方法是什么吗?

xml string powershell

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

使用 Python 3.5 执行 PowerShell 命令

我一直在研究一个需要通过 PowerShell 命令行运行 Python 脚本的问题。脚本应该将命令传递到命令行并保存输出。但是,我遇到了无法识别某些命令行参数的问题。

import subprocess
try:
    output = subprocess.check_output\
    (["Write-Output 'Hello world'"], shell=True)
    # (["dir"], shell=True)
except subprocess.CalledProcessError as e:
    print(e.output)
    print('^Error Output^')
Run Code Online (Sandbox Code Playgroud)

如果我将当前命令与命令一起使用check_output,我会收到一条错误消息,指出:

'"Write-Output 'Hello world'"' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

如果我只使用该"dir"行,脚本运行得很好。我在这里不同意为什么会发生这种情况。这不是我正在运行的确切脚本,但它在我的机器上产生了同样的问题。如果我只是在命令行中输入问题命令,它会"Hello world"按预期输出到新行。

任何有关为什么会发生这种情况的见解将不胜感激。如果相关,我不想使用任何类型的管理员权限解决方法。

python powershell windows-scripting

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

PowerShell 功能和性能

我一直想知道 PowerShell 中函数对性能的影响。假设我们想使用 System.Random 生成 100.000 个随机数。

$ranGen = New-Object System.Random
Run Code Online (Sandbox Code Playgroud)

执行

for ($i = 0; $i -lt 100000; $i++) {
    $void = $ranGen.Next()
}
Run Code Online (Sandbox Code Playgroud)

在 0.19 秒内完成。我把调用放在一个函数中

Get-RandomNumber {
    param( $ranGen )

    $ranGen.Next()
}
Run Code Online (Sandbox Code Playgroud)

执行

for ($i = 0; $i -lt 100000; $i++) {
    $void = Get-RandomNumber $ranGen
}
Run Code Online (Sandbox Code Playgroud)

大约需要 4 秒。

为什么会有如此巨大的性能影响?

有没有办法可以在使用函数的同时获得直接调用的性能?

PowerShell 中是否有更好(更高效)的代码封装方式?

powershell

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

无法在 Powershell 中安装 SQLServer 模块

我尝试通过以管理员身份(win10,64Bit)执行以下命令来安装 Powershell SQLSERVER 模块,但它失败了

PS C:\WINDOWS\system32> Install-Module -Name SqlServer
PackageManagement\Install-Package : No match was found for the specified search 
criteria and module name 'SqlServer'.
Try Get-PSRepository to see all available registered module repositories.
At C:\Program 
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 
char:21
+ ...          $null = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: 
(Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex
   ception
    + FullyQualifiedErrorId : 

NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.
InstallPackage
Run Code Online (Sandbox Code Playgroud)

我在网上搜索过,尝试了一些解决方案,我什至与公司管理员交谈过,但没有运气。正如他们告诉我的那样,公司没有代理,只有防火墙。其他同事可以毫无问题地运行该命令。下一个命令也失败

PS C:\WINDOWS\system32> Get-PSrepository
WARNING: Unable to find module repositories.
Run Code Online (Sandbox Code Playgroud)

任何的想法?

sql-server powershell sqlps

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

简单的 jq 过滤器在 Windows shell 中不起作用,各种引用问题

我真的很努力地让 Windows shell 与 jq 一起工作并且失败了。

我希望这种类型的东西起作用

echo '["a","b","c"]' | .\Downloads\jq.exe -r '{ "data": map({ "{#SNAME}": . })}'
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

jq:错误:语法错误,意外的“。” (Windows cmd shell 引用问题?)在第 1 行:

如果我只是这样做,echo '["a","b","c"]' | .\Downloads\jq.exe -r '.'那么它很高兴,但是一旦我添加其他字符,echo '["a","b","c"]' | .\Downloads\jq.exe -r '{.}'它就会再次失败:

jq:错误:语法错误,意外的“。” (Windows cmd shell 引用问题?)在第 1 行:

有谁知道如何让 Windows shell 对 jq 感到满意,以使上述示例按预期工作?

使用来自 jq 网站的最新 1.6 版本进行这些测试,并确认 jq 命令使用 Linux 和 jqplay.org 工作。

谢谢你。

windows powershell cmd jq

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