标签: powershell

如何在第一个错误上停止PowerShell脚本?

我希望我的PowerShell脚本在我运行的任何命令失败时停止(例如set -e在bash中).我正在使用Powershell命令(New-Object System.Net.WebClient)和程序(.\setup.exe).

windows powershell

226
推荐指数
8
解决办法
13万
查看次数

使用PowerShell以UTF-8编写文件而不使用BOM

Out-File 似乎在使用UTF-8时强制BOM:

$MyFile = Get-Content $MyPath
$MyFile | Out-File -Encoding "UTF8" $MyPath
Run Code Online (Sandbox Code Playgroud)

如何使用PowerShell以UTF-8编写没有BOM的文件?

powershell encoding byte-order-mark utf-8

225
推荐指数
9
解决办法
21万
查看次数

Powershell Invoke-WebRequest使用SSL/TLS安全通道失败

我正在尝试执行此powershell命令

Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

我收到这个错误."Invoke-WebRequest:请求已中止:无法创建SSL/TLS安全通道." https请求似乎有效(" https://google.com ")但不是这个问题.如何让它工作或使用其他powershell命令来读取页面内容?

powershell ssl

219
推荐指数
5
解决办法
11万
查看次数

PowerShell中的$ _是什么意思?

我在PowerShell中看到过以下内容,但它究竟做了什么?

$_
Run Code Online (Sandbox Code Playgroud)

powershell

218
推荐指数
4
解决办法
21万
查看次数

使用PowerShell遍历目录中的文件

如何更改以下代码以查看目录中的所有.log文件而不仅仅是一个文件?

我需要遍历所有文件并删除所有不包含"step4"或"step9"的行.目前这将创建一个新文件,但我不知道如何在for each这里使用循环(新手).

实际文件的名称如下:2013 09 03 00_01_29.log.我希望输出文件覆盖它们,或者具有SAME名称,附加"out".

$In = "C:\Users\gerhardl\Documents\My Received Files\Test_In.log"
$Out = "C:\Users\gerhardl\Documents\My Received Files\Test_Out.log"
$Files = "C:\Users\gerhardl\Documents\My Received Files\"

Get-Content $In | Where-Object {$_ -match 'step4' -or $_ -match 'step9'} | `
Set-Content $Out
Run Code Online (Sandbox Code Playgroud)

powershell

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

使用PowerShell创建硬链接和软链接

PowerShell 1.0可以创建类似于Unix的硬链接和软链接吗?

如果这不是内置的,有人可以指向我有一个模仿这个的ps1脚本的网站吗?

这是任何好壳,恕我直言的必要功能.:)

powershell symlink

215
推荐指数
8
解决办法
15万
查看次数

PowerShell中的Echo等效脚本测试

我想通过设置标志并在整个脚本中查看数据来输出PowerShell脚本中的变量和值.

我该怎么做?

例如,PowerShell与以下PHP代码等效的是什么?

echo "filesizecounter: " . $filesizecounter 
Run Code Online (Sandbox Code Playgroud)

powershell

214
推荐指数
10
解决办法
62万
查看次数

在PowerShell中,如何在文件中定义函数并从PowerShell命令行调用它?

我有一个.ps1文件,我想在其中定义自定义函数.

想象一下,该文件名为MyFunctions.ps1,内容如下:

Write-Host "Installing functions"
function A1
{
    Write-Host "A1 is running!"
}
Write-Host "Done"
Run Code Online (Sandbox Code Playgroud)

要运行此脚本并理论上注册A1函数,我导航到.ps1文件所在的文件夹并运行该文件:

.\MyFunctions.ps1
Run Code Online (Sandbox Code Playgroud)

这输出:

Installing functions
Done
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试调用A1时,我只是得到错误,指出该名称没有命令/功能:

The term 'A1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
 of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:3
+ A1 <<<<
    + CategoryInfo          : ObjectNotFound: (A1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

我必须误解一些PowerShell概念.我可以不在脚本文件中定义函数吗?

请注意 …

powershell

214
推荐指数
6
解决办法
22万
查看次数

如何在PowerShell中静默删除包含内容的目录

使用PowerShell,是否可以删除包含文件的某个目录而不提示确认操作?

directory powershell

213
推荐指数
9
解决办法
25万
查看次数

如何在Powershell中解压缩文件?

我有一个.zip文件,需要使用Powershell解压缩整个内容.我这样做但它似乎不起作用:

$shell = New-Object -ComObject shell.application
$zip = $shell.NameSpace("C:\a.zip")
MkDir("C:\a")
foreach ($item in $zip.items()) {
  $shell.Namespace("C:\a").CopyHere($item)
}
Run Code Online (Sandbox Code Playgroud)

怎么了?该目录C:\a仍为空.

powershell

213
推荐指数
8
解决办法
22万
查看次数

标签 统计

powershell ×10

byte-order-mark ×1

directory ×1

encoding ×1

ssl ×1

symlink ×1

utf-8 ×1

windows ×1