标签: powershell

PowerShell运行"dotnet run ..."后没有返回命令行

我目前正在尝试构建一个PowerShell脚本,需要将我自己的C#应用​​程序作为服务启动,然后我需要检查事件日志中的错误或其他条目.但是一旦完成了dotnet运行任务,该进程就不会让我运行另一个命令,它会等待启动进程停止.

这是PowerShell脚本的一个示例

CD C:\MyCSharpProject\Project\ 
dotnet run --debug <# script stop after this command is run, and the next command is not run r#>
notepad <# This line is never hit #>
Run Code Online (Sandbox Code Playgroud)

如何启动我的服务然后再运行另一个命令?

c# powershell service

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

PowerShell Get-ChildItem如何捕获异常

我目前正在编写一个可视错误GUI,在处理过程中捕获任何异常,并为用户提供"易于理解"的错误消息.但似乎在使用Get-ChildItemcmdlet 时我无法捕获任何异常.我是否必须使用与try/catch不同的方法?

这是PowerShell脚本:

if ($checkBox1.Checked)    {
    Try{
        Get-ChildItem -path K:\adm_spm_logdb_data\ADP\DATA |Rename-Item -newname { $($_.BaseName -split '_provide')[0] + $_.Extension };
        }catch [System.Exception]{
        $listBox1.Items.Add("An error occured while trying to process ADP-Files please check the manual!")
        }
        $listBox1.Items.Add("Please check your System files to ensure the process has finished")
    }
Run Code Online (Sandbox Code Playgroud)

我试图通过使用虚假创建例外-path,其导致DriveNotFoundException.但看起来我无法通过使用try/catch来捕获它.

powershell exception try-catch

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

如何在PowerShell脚本之后阻止Excel进程在后台运行?

无论我尝试什么,Excel 2013都会继续在Windows 10的后台运行,无论我在PowerShell脚本末尾抛出什么命令.我已经尝试将我发现的所有建议添加到我的脚本末尾,并且我打开的唯一Excel对象仍然保持打开状态.这是我脚本末尾的内容.还有其他建议吗?

## Quit Excel and Terminate Excel Application process:
$xlsxwb.quit
$xlsxobj.Quit

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsxobj)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsxwb)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsxSh1)
Start-Sleep 1
'Excel processes: {0}' -f @(Get-Process excel -ea 0).Count
Run Code Online (Sandbox Code Playgroud)

excel powershell excel-2013 windows-10

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

使用PowerShell的Invoke-RestMethod的OneLogin REST API

我正在使用OneLogin REST API,似乎无法使用PUT方法进行任何调用.当我在Postman中测试时,我可以传递一个原始的JSON体,如下所示:

{
   "role_id_array":  [
                        115028
                     ]
}
Run Code Online (Sandbox Code Playgroud)

到端点:

https://api.us.onelogin.com/api/1/users//add_roles

这很好用.但是,当我尝试使用PowerShell的Invoke-RestMethod命令执行相同操作时,我收到400 Bad Request错误.我的代码看起来像这样:

$Splat = @{
    Method      = $Method
    Uri         = https://api.us.onelogin.com/api/1/users/12345678/add_roles
    ContentType = "application/json"
    Headers     = @{authorization = "bearer:$($Token.access_token)"}
    Body        = @{role_id_array = @(123456)}
}

Invoke-RestMethod @Splat
Run Code Online (Sandbox Code Playgroud)

到目前为止,我对任何GET调用没有任何问题,只有PUT.另外,如果我运行我通过ConvertTo-Json传入的Body哈希表,输出看起来就像上面的工作示例.有没有人对为什么这不起作用有任何想法?

rest powershell onelogin

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

PowerShell Git diff传递和传递参数

我创建了一个简单的PowerShell脚本来自动创建包含在两个git标签之间修改的所有文件的存档.

它似乎与我所做的一些测试工作得很好,但今天一位同事抱怨说它没有添加一个特定的文件.我开始调试它,我看到一个非常奇怪的行为.

正如您所看到的,我用来列出修改过的文件的命令是git diff,它传递了我从PowerShell命令的参数(作为字符串)获得的标签.

param(
    # Git tag or commit has from which we want the patch to start
    [String] $FromTag,
    # Path of the patch file
    [String] $PatchFilePath,
    # Git tag or commit hash where to we want the patch to update to (default HEAD, current checkout)
    [String] $ToTag = "HEAD"
)
...
$modifiedFiles = git diff $FromTag..$ToTag --no-commit-id --name-only --diff-filter=ACMRT 
...
Run Code Online (Sandbox Code Playgroud)

测试它,我看到文件确实丢失了.我决定手动运行命令,如下所示:

git diff v041.3.2..v041.3.6 --no-commit-id --name-only --diff-filter=ACMRT
Run Code Online (Sandbox Code Playgroud)

哪个归还了文件...让我感到困惑.然后我尝试执行以下操作:

$FromTag = "v041.3.2"
$ToTag = "v041.3.6"
git diff $FromTag..$ToTag …
Run Code Online (Sandbox Code Playgroud)

git powershell powershell-4.0

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

连接路径和文件

我是PowerShell的新手,我很高兴我在编写新功能等方面有多远,但是这个让我难过,这么简单......

以下是功能:

Function fnConcatenatePathFile ([String]$Path, [String]$File)
{
   $FullPath = "$($Path)\$($File)"
   Return $FullPath
}
Run Code Online (Sandbox Code Playgroud)

然后我调用这个函数:

fnConcatenatePathFile ("C:\_Store", "run.sql")
Run Code Online (Sandbox Code Playgroud)

我希望结果如下:

"C:\_Store\run.sql"
Run Code Online (Sandbox Code Playgroud)

但相反,我得到:

"C:\_Store run.sql\"
Run Code Online (Sandbox Code Playgroud)

让我疯狂......任何线索?

variables powershell function

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

PowerShell启动过程未设置$ lastexitcode

我有一组测试DLL,它们是通过Start-Process命令通过OpenCover.Console.exe的Powershell脚本运行的。

我-returntargetcode设置了标志

执行后,我检查$lastexitcode和$?。他们一直都分别返回1和True。即使测试失败。

$lastexitcode当所有测试通过时不应该为0,而在所有测试失败时是否应该为1?

powershell

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

修复`Set-WSManQuickConfig`的“无法检查防火墙状态”

为什么Enable-PSRemoting/ Set-WSManQuickConfig无法在Windows 2012R2,Azure WebRole的管理员下运行“检查防火墙的状态”?如何解决这个问题?

PS D:\Users\***User> enable-psremoting -force

WinRM is already set up to receive requests on this computer.
Set-WSManQuickConfig : <f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="2"
Machine="localhost"><f:Message><f:ProviderFault provider="Config provider"
path="%systemroot%\system32\WsmSvc.dll"><f:WSManFault xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault"
Code="2" Machine="RD***CA2"><f:Message>Unable to check the status of the firewall.
</f:Message></f:WSManFault></f:ProviderFault></f:Message></f:WSManFault>
At line:69 char:17
+                 Set-WSManQuickConfig -force
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Set-WSManQuickConfig], InvalidOperationException
    + FullyQualifiedErrorId : WsManError,Microsoft.WSMan.Management.SetWSManQuickConfigCommand
Run Code Online (Sandbox Code Playgroud)

操作系统名称:Microsoft Windows Server 2012 R2数据中心。操作系统版本:6.3.9600 N / A Build 9600. Powershell:4.0

PS。是的,有:

powershell azure

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

Foreach-Object vs Foreach循环的运行时

我想做我的脚本的进度条,但后来我需要总量的文件夹.

是否存在显着的运行时差异:

Get-ChildItem $path -Directory | ForEach-Object {
    #do work
}
Run Code Online (Sandbox Code Playgroud)

和

$folders = Get-ChildItem $path -Directory
foreach($folder in $folders){
    #do work
}
Run Code Online (Sandbox Code Playgroud)

然后我可以$folders.Count用作我的文件夹总数.我不知道如何使用foreach-object循环来完成它.

powershell

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

获取CMD/POWER Shell中的CPU温度

在我的电脑中,我试图获得CPU温度.在StackOverflow上搜索我发现了这个:

C:\WINDOWS\system32>wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

Node - ADMIN
ERROR:
Description = Not supported
Run Code Online (Sandbox Code Playgroud)

cpu powershell cmd temperature

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