这个名字背后的原因是什么?
SS64在PowerShell中解释了这里的字符串如下:
此处字符串是单引号或双引号字符串,可以跨越多行.不评估单引号字符串中的表达式.
here-string中的所有行都被解释为字符串,即使它们没有用引号括起来.
Run Code Online (Sandbox Code Playgroud)$myHereString = @' some text with "quotes" and variable names $printthis some more text '@
这是我的代码,它不起作用:
$brisanje=dat.txt, dat2.txt
Remove-Item $brisanje
Run Code Online (Sandbox Code Playgroud)
我在这里得到的错误是
Remove-Item:找不到路径'C:\ Users\stefan\Desktop\brisanjedat\dat.txt,dat2.txt'因为它不存在.
但是当我像这样写它时,它就像一个魅力:
Remove-Item dat.txt, dat2.txt
Run Code Online (Sandbox Code Playgroud)
几个小时我一直坚持这个问题,任何解决方案?
是否可以使用shell获得休眠状态?
我正在使用powercfg.exe启用/禁用它,并且似乎没有任何方法来获取状态,即使使用/ Q(查询).
我也尝试将其存在,%SystemDrive%\hiberfil.sys但需要管理员权限.
我想在自动化PowerShell脚本中进行多次测量。我使用了Get-Date和TotalSeconds。结果看起来像这样:236.908
如何以分钟和秒为单位获得经过的时间?
$startTime = (Get-Date)
$endTime = (Get-Date)
$ElapsedTime = (($endTime-$startTime).TotalSeconds)
Write-Host "Duration: xx min xx sec"
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码
$WebClient = New-Object system.net.webclient
$WebClient.credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password
$WebClient.Proxy = $null
$WebClient.Headers.Add("COperation","MethodCall")
$WebClient.Headers.Add("CMethod", "EnumerateInstances")
$WebClient.Headers.Add("CObject", $NameSpace)
$WebClient.Headers.Add("Content-Type", "application/xml")
$System= $WebClient.UploadString($Url, "POST", $EnumMessage)
Run Code Online (Sandbox Code Playgroud)
这很好用.我想要做的是将安全协议设置为Tls1.2或Tls1.1.请帮忙.
如标题所示,我在舍入数字时遇到一些问题。我的脚本当前如下所示:
[uint16]$Product1 = Read-Host "Enter the price of your product: "
...
#$TotalProducts contains the Value from all products together
Write-Host "You spent an amount of $TotalProducts for todays shopping."
Run Code Online (Sandbox Code Playgroud)
我希望程序将数字四舍五入,以使总数不是一个荒谬的长数字。它确实可以工作,但是在我手动计算之后,我看到程序计算了其他内容。
这里的问题是程序将例如122.50舍入为122,而不是123。
我尝试使用以下语法,但未成功:
[math]::Round($Product1)[System.Midpoint.Rounding]::AwayFromZero) = Read-Host "Enter the price of your product: "
Run Code Online (Sandbox Code Playgroud)
我是在尝试正确的方法,但只是在挑剔语法,还是这种方法完全错误?
我使用以下命令来运行管道.
.\Find-CalRatioSamples.ps1 data16 `
| ? {-Not (Test-GRIDDataset -JobName DiVertAnalysis -JobVersion 13 -JobSourceDatasetName $_ -Exists -Location UWTeV-linux)}
Run Code Online (Sandbox Code Playgroud)
第一个是我的自定义脚本,运行速度非常快(毫秒).第二个是自定义命令,也是由我编写的(参见https://github.com/LHCAtlas/AtlasSSH/blob/master/PSAtlasDatasetCommands/TestGRIDDataset.cs).这很慢.
实际上,处理每一行输入并不是那么慢.可以处理第一行输入之前的设置非常昂贵.但是,这样做很快.因此,所有昂贵的代码都会执行一次,并且只需要为每个新的管道输入执行相当快的代码.
不幸的是,当我想做的时候?上面的{}构造,似乎PowerShell不像以前那样保留管道.现在它命令我为每行输入命令一个新的时间,导致命令重做每一行的所有设置.
在我如何调用管道线路方面有什么可以改变的吗?或者我如何编写我的cmdlet来防止这种情况发生?或者我被卡住,因为这只是Where-Object的工作方式?
我想为powershell脚本中的文件路径生成一个字符串.我希望这在windows和mac中同时工作.
目前,代码被硬编码到类似路径的窗口("\" - > windows,"/" - > unix):
$templatep="$CoreRoot\templates\$serviceName"
我将其更改为:
$templatep= Join-Path $CoreRoot "templates" $serviceName
并且它在Mac中使用Powershell 6.0.但是它在我的带有Powershell 4的Windows服务器中不起作用.我必须做这样的事情:
$templatep= Join-Path $CoreRoot -ChildPath "templates" | Join-Path -ChildPath $serviceName
知道为什么这只是在我的Mac上工作吗?这是PowerShell 5还是6的新功能?我不喜欢必须管道多个Join-Paths.有一个更好的方法吗?
谢谢!
我正在开发一个PowerShell脚本,我在其中输入一个长字符串(来自CSV文件),格式如下:
第一组名称
组二名
组三名
...
我正在尝试解析它
($entry.'Group Name').split("`n ") | %{
if ($_) {
# Do something with the group name
$_
}
}
Run Code Online (Sandbox Code Playgroud)
我希望获得如下输出:
第一组名称
组二名
组三名
...
但它出现了:
集团
的一个
名称
组
两个
...
我有一个工作脚本,其目的是在导入Oracle之前解析格式错误的行的数据文件.要处理450MB csv文件,其中包含大于1百万行的8列,需要2.5小时,最多只需一个CPU内核.小文件快速完成(以秒为单位).
奇怪的是,具有相似行数和40列的350MB文件只需要30分钟.
我的问题是文件会随着时间的推移而增长,并且2.5小时占用CPU并不好.谁能推荐代码优化?一个类似的标题帖推荐了本地路径 - 我已经在做了.
$file = "\Your.csv"
$path = "C:\Folder"
$csv = Get-Content "$path$file"
# Count number of file headers
$count = ($csv[0] -split ',').count
# https://blogs.technet.microsoft.com/gbordier/2009/05/05/powershell-and-writing-files-how-fast-can-you-write-to-a-file/
$stream1 = [System.IO.StreamWriter] "$path\Passed$file-Pass.txt"
$stream2 = [System.IO.StreamWriter] "$path\Failed$file-Fail.txt"
# 2 validation steps: (1) count number of headers is ge (2) Row split after first col. Those right hand side cols must total at least 40 characters.
$csv | Select -Skip 1 | % {
if( ($_ -split ',').count -ge $count -And …Run Code Online (Sandbox Code Playgroud) powershell ×10
cmd ×1
csv ×1
if-statement ×1
math ×1
naming ×1
performance ×1
rounding ×1
string ×1
unix ×1
webclient ×1
windows ×1