我正在将一些 Excel 计算转换为 Powershell:
=ROUNDDOWN((250000.00-0)*5.00%/12,6)
Run Code Online (Sandbox Code Playgroud)
这给出了一个值 1041.666666
我已将其转换为 Powershell:
[Math]::Round((250000.00 - 0) * (5.00/100) / 12,6)
Run Code Online (Sandbox Code Playgroud)
但这给出了一个值 1041.666667
问题在于最后一个数字很重要,而且 powershell 没有将小数点四舍五入7到6. 这会导致问题,因为 Excel 的最终结果是,1461.59但对于 powershell 是1461.42
那么,在 Powershell 中将正确舍入到一定数量的小数位的最佳方法是什么?
我遇到了从文件中提取文本的问题。这是我最终想要做的:
这是我正在处理的部分的片段:
$FileContents = Select-String -Path "C:\Users\me\Documents\file.ini" -Pattern "NoRequest"
Run Code Online (Sandbox Code Playgroud)
当我使用该行时,我得到以下信息:
PS> $文件内容 Documents\file.ini:222:NoRequest = false Documents\file.ini:281:NoRequest = false Documents\file.ini:347:NoRequest = false Documents\file.ini:387:NoRequest = false Documents\file.ini:1035:NOREQUEST = true Documents\file.ini:1047:NoRequest = true Documents\file.ini:1160:NOREQUEST = true Documents\file.ini:1242:NOREQUEST = true
这就是我想要得到的:
PS> $文件内容 错误的 错误的 错误的 错误的 真的 真的 真的 真的
我尝试将代码通过管道传输到Select-Object -Last 1解决方案中,但由于我的变量是一个数组,因此它选择数组中的最后一行,而不是每行的最后一个字。我还想过只取数组并执行“数组中的 foreach 行,将除最后一个字外的所有内容都剪掉”。我猜这会奏效,但似乎这是一个笨拙的解决方案。我确信有一个简单有效的解决方案,但我只是没有看到。
在我公司的电脑中,当我在 powershell 中输入内容后按 Tab 键时,它会显示一个建议列表,我可以使用箭头键选择列表中的项目。例如,这是我键入“fin”然后按 tab 时的结果:
如何配置 powershell 以显示该列表?我尝试将 powershell 配置文件复制到我的笔记本电脑并导入必要的模块,但它不像图片那样工作(外壳只显示与我键入的内容匹配的第一件事,对于上面的示例,它显示了Find-Command我键入fin并按 Tab 的时间,但不是列表)
这是个人资料:
function Test-Administrator {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Sorin
Import-Module Get-ChildItemColor
Set-Alias l Get-ChildItemColor -option AllScope
Set-Alias ls Get-ChildItemColorFormatWide -option AllScope
function cddash {
if ($args[0] -eq '-') {
$pwd = $OLDPWD;
} else {
$pwd = $args[0];
}
$tmp = pwd;
if ($pwd) {
Set-Location $pwd;
}
Set-Variable -Name OLDPWD -Value $tmp -Scope …Run Code Online (Sandbox Code Playgroud) 我需要删除除根文件夹中的一个文件和子文件夹中的另一个文件之外的所有文件和文件夹。此外,文件名作为参数以逗号分隔的字符串形式传递给脚本,如“file1.txt,Subfolder\file2.txt”。
我试图做这样的事情,
$Path = "C:\\Delete\\"
$Argument= "file1.txt,Subfolder\\file2.txt"
$ExcludedFiles = [string]::Join(',', $Argument);
$files = [System.IO.Directory]::GetFiles($Path, "*", "AllDirectories")
foreach($file in $files) {
$clearedFile = $file.replace($Path, '').Trim('\\');
if($ExcludedFiles -contains $clearedFile){
continue;
}
Remove-Item $file
}
Run Code Online (Sandbox Code Playgroud)
通过这样做,所有文件夹都保留下来,所有文件都被删除。任何人都可以建议我应该如何尝试这样做,因为我很难做到这一点。
我正在尝试从 powershell 脚本运行以下命令。
"C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" /create "C:\test veracrypt file.hc" /password alongpasswordisagoodpassword /hash sha512 /encryption serpent /filesystem NTFS /size 100G /dynamic /force /silent
Run Code Online (Sandbox Code Playgroud)
我试过使用
& cmd.exe ""C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" /create "C:\test veracrypt file.hc" /password alongpasswordisagoodpassword /hash sha512 /encryption serpent /filesystem NTFS /size 100G /dynamic /force /silent"
Run Code Online (Sandbox Code Playgroud)
和
$command = @'
& cmd.exe ""C:\Program Files\VeraCrypt\VeraCrypt Format-x86.exe" /create "C:\test veracrypt file.hc" /password
alongpasswordisagoodpassword /hash sha512 /encryption serpent /filesystem NTFS /size 100G /dynamic /force /silent"
'@
Invoke-Expression -Command:$command
Run Code Online (Sandbox Code Playgroud)
我所做的一切都会出现错误:
cmd.exe : 'reate' 不是内部或外部命令, …
我想同步两个 AzureAD 组,所以我用Get-AzureADGroupMember.
问题是,我只能从小组中取出 103 个人而不是 615 人...
我该怎么做才能从组中取出整个列表?
提前致谢,罗宾
我尝试了几个选项来迭代我的目录并在以下命令之间获得巨大的性能差异:
减缓:
Get-ChildItem -Directory -Force -Recurse -Depth 3 -Include '$tf'
Run Code Online (Sandbox Code Playgroud)
快速地:
Get-ChildItem -Directory -Force -Recurse -Depth 3 | Where-Object Name -eq '$tf'
Run Code Online (Sandbox Code Playgroud)
有人能解释一下为什么第一个语句比第二个语句慢得多吗?
我正在尝试编写一个 PowerShell 脚本来启动我的开发环境所需的所有服务,并且正在努力寻找一种方法来启动 azure 存储模拟器。我所需要的只是一种启动服务的方法,但我在网上阅读的所有内容都不起作用。
提前致谢。
我正在尝试获取第一块发行说明...
(请参阅代码中的示例内容)
每当我使用一些简单的东西时,
它只会在我尝试跨多行 ( \n)搜索时中断。
我正在使用,(Get-Content $changelog | Out-String)因为它返回 1 个字符串而不是每行的数组。
$changelog = 'C:\Source\VSTS\AcmeLab\AcmeLab Core\changelog.md'
$regex = '([Vv][0-9]+\.[0-9]+\.[0-9]+\n)(^-.*$\n)+'
(Get-Content $changelog | Out-String) | Select-String -Pattern $regex -AllMatches
<#
SAMPLE:
------
v1.0.23
- Adds an IContainer API.
- Bugfixes.
v1.0.22
- Hotfix: Language operators.
v1.0.21
- Support duplicate query parameters.
v1.0.20
- Splitting up the ICommand interface.
- Fixing the referrer header empty field value.
#>
Run Code Online (Sandbox Code Playgroud)
我需要的结果是:
v1.0.23
- Adds an IContainer API.
- Bugfixes.
Run Code Online (Sandbox Code Playgroud)
更新:
使用 …
我正在查看 PowerShell 的Rename-Itemcmdlet的文档,有一个这样的例子。
Get-ChildItem *.txt | Rename-Item -NewName { $_.name -Replace '\.txt','.log' }
Run Code Online (Sandbox Code Playgroud)
此示例说明如何使用 Replace 运算符重命名多个文件,即使 NewName 参数不接受通配符也是如此。
此命令将当前目录中的所有 .txt 文件重命名为 .log。
该命令使用 Get-ChildItem cmdlet 获取当前文件夹中具有 .txt 文件扩展名的所有文件。然后,它使用管道运算符 (|) 将这些文件发送到 Rename-Item 。
NewName 的值是在将值提交给 NewName 参数之前运行的脚本块。
注意最后一句话:
NewName 的值是在将值提交给 NewName 参数之前运行的脚本块。
实际上NewName是一个字符串:
[-NewName] <String>
Run Code Online (Sandbox Code Playgroud)
那么这是否意味着当所需的参数类型是字符串时,我总是可以使用脚本块?
powershell ×10
azure ×2
arrays ×1
cmd ×1
cmdlets ×1
delay-bind ×1
foreach ×1
parameters ×1
regex ×1
scriptblock ×1
storage ×1
variables ×1
veracrypt ×1