标签: powershell

Powershell - 在函数中执行命令之前确认

我将以下函数放入我的 powershell 配置文件中(来自 的路径echo $profile,我的就像D:\C_Drive\Hardlink\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1)

function test {
    [cmdletbinding(SupportsShouldProcess)]
    ls
}
Set-Alias ggg test
Run Code Online (Sandbox Code Playgroud)

提示确认[cmdletbinding(SupportsShouldProcess)],但运行时出现以下错误ggg

At D:\C_Drive\Hardlink\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:2 char:5
+     [cmdletbinding(SupportsShouldProcess)]
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected attribute 'cmdletbinding'.
At D:\C_Drive\Hardlink\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:3 char:5
+     ls
+     ~~
Unexpected token 'ls' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedAttribute
Run Code Online (Sandbox Code Playgroud)

在函数中执行命令之前确认的正确方法是什么?

powershell

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

不同长度的数组到一个 CSV

如果您有多个不同长度的数组,如何将它们导出到 powershell 中的单个 csv 中?

\n
Array1 = 1,2,3 \nArray2 = Bob,smithy,Alex,Jeremy \nArray3 = yes,no\n
Run Code Online (Sandbox Code Playgroud)\n

输出 CSV

\n
\n号码名称有效\n\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2 \x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80 \x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94 \n1 鲍勃 是 \n2 史密斯 没有 \n3 亚历克斯\n 杰里米
\n

基本上每个数组都位于其自己的标题列中。

\n

尝试过类似的行

\n
Array1 | Select-Object Number | export-csv -Path C:\\Path\n
Run Code Online (Sandbox Code Playgroud)\n

这适用于单一数组到单一 csv 文件

\n

但如果我尝试

\n
Array1, Array2, Array3 | Select-Object Number, Name, Valid | export-csv -Path C:\\Path\n
Run Code Online (Sandbox Code Playgroud)\n

我只得到标题名称,列中没有值

\n

arrays powershell export-to-csv

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

在 powershell 管道中,如何等待 cmdlet 完成后再继续下一个?

考虑以下函数:

function myFunction {
  100
  sleep 1
  200
  sleep 1
  300
  sleep 1
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它将沿着管道一一发出这些值。

但我想等待所有值发出后再继续。喜欢

myFunction | waitForThePreviousCommandToComplete | Format-Table 
Run Code Online (Sandbox Code Playgroud)

我希望上面的格式表接收整个数组,而不是一对一的项目。

在 Powershell 中是否可以实现?

powershell

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

Powershell - 如何读取 Ansi CSV 文件

Notepad++ 表示 CSV 文件是 Ansi 编码的。

Powershell 7 Import-CSV commandlet 有各种 -Encoding 选项,但“Ansi”不是其中之一。

如何让 Powershell 读取此 CSV 而不会损坏它?

-Encoding 的选项有:

  • ASCII码
  • bigendianunicode
  • bigendianutf32
  • 贴牌生产
  • 统一码
  • UTF7
  • UTF8
  • utf8BOM
  • utf8无BOM
  • UTF32

powershell powershell-7.2

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

使用 CMD 和 PowerShell 以静默方式安装 exe 文件

我正在尝试安装使用 Inno Setup 创建的 exe。当我尝试使用 PowerShell 安装它时

Start-Process -wait C:\updatefile.exe /VERYSILENT
Run Code Online (Sandbox Code Playgroud)

所有文件夹都创建在正确的位置,创建注册表值,但控制台挂起并且不会返回,除非Ctrl+C按下 。

但是如果我使用命令提示符安装 exe

start /wait C:\updatefile.exe /VERYSILENT
Run Code Online (Sandbox Code Playgroud)

一切正常,命令提示符返回。

造成这种异常现象的原因可能是什么?

我需要通过 Dockerfile 安装此 exe,但由于 PowerShell 安装挂起,容器不会安装 exe。即使使用cmdDockerfile 中的版本,容器也不会创建安装文件夹。


检查日志后我发现exe服务最终由安装程序启动。如果这是挂起的原因,是否有一些参数可以用来退出 PowerShell?

powershell cmd inno-setup docker

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

如果 2 秒后没有结果,则转到下一个命令

我有大量的 PC 号码列表(大约 2000+-),我使用以下命令进行查询:

$Computers = get-content = "C:\Users\Public\Documents\CSV\tfiles\Computernames.txt"

foreach ($Computer in $Computers) {
    query user /server:$Computer
}
Run Code Online (Sandbox Code Playgroud)

我想知道如果 2 秒后没有 PC 名称结果,我是否可以通过跳过命令来加快此过程。

也欢迎其他更快的方法。

powershell performance

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

Windows 托管运行器上 Github Actions 的输出

我想在 Windows 自托管运行器上访问之前作业的步骤结果。为了做到这一点,我使用如下输出变量,效果很好(在最后一步中打印“成功”)。

name: Debug workflow

on: workflow_dispatch
jobs:
  job1:
    runs-on: tester-1
    outputs:
      output1: ${{ steps.step1.outputs.MY_OUTPUT }}
    steps:
      - name: Checkout with submodules
        id: checkout_step
        uses: actions/checkout@v3
      - name: Write variable to output
        id: step1
        run: echo '::set-output name=MY_OUTPUT::${{ steps.checkout_step.outcome }}'
  job2:
    runs-on: tester-1
    needs: job1
    env:
      OUTPUT1: '${{needs.job1.outputs.output1}}'
    steps:
    - name: Print outputs from previous 
      run: echo ${{ env.OUTPUT1 }}
Run Code Online (Sandbox Code Playgroud)

然而::set-output 已被弃用,所以我想转换为正确的方法。根据另一个问题,我已经尝试将我的替换echo '::set-output name=MY_OUTPUT::${{ steps.checkout_step.outcome }}'为

  1. echo "MY_OUTPUT=${{ steps.checkout_step.outcome }}" >> $env:GITHUB_ENV
  2. echo …

powershell github github-actions

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

如何在 PowerShell 中禁用 Internet Explorer 解析

Invoke-WebRequest如果 Internet Explorer 不可用,cmdlet 可能会失败* :

The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.
Run Code Online (Sandbox Code Playgroud)

如果我使用的计算机具有正常运行的 Internet Explorer,-UseBasicParsing则不需要。我想测试我的脚本是否可以在未运行 IE 或已卸载 IE 的计算机上运行。在我的测试环境中,如何刻意创造发生上述错误的条件呢?

* PowerShell 6.0.0 之前。

powershell

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

是否可以使用其命名空间和名称来调用由 Add-Type 添加的函数?

我正在使用这样的内核函数:

$defText = '[DllImport("kernel32.dll")]public static extern uint SetThreadExecutionState(uint esFlags);'
$def = Add-Type -MemberDefinition $defText -PassThru -Namespace myNameSpace -Name myName
$def::SetThreadExecutionState(0)
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以通过名称空间和名称调用该方法(不使用 -PassThru)?

$defText = '[DllImport("kernel32.dll")]public static extern uint SetThreadExecutionState(uint esFlags);'
Add-Type -MemberDefinition $defText -Namespace myNameSpace -Name myName
myNameSpace::myName(0)
Run Code Online (Sandbox Code Playgroud)

当然它说没有找到该命令。是否可以通过名称空间和名称来调用方法?

powershell

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

-replace,如何只匹配第一个点?

$Var = "Dog.Jumping.png", "Horse.Jumping.gif", "Cat.Jumping.jpg", "Lion.Jumping.tiff", "Shark.Jumping.svg", "Cow.Jumping.psd"
$Var -replace '\.', ' - '
Run Code Online (Sandbox Code Playgroud)

上面好像找到了两个“.” 角色,我只对第一个感兴趣。类似的事情$Var -replace '\.?', ' - '没有道理,我真的被难住了。

任何帮助将不胜感激!

powershell

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