我的电脑上有两个版本,因为我正在学习不同的教程,(我还是个菜鸟)。我在这里看到了几个类似的问题,但没有特定于 Powershell 的问题。
当我在 Powershell 中运行python 时,它会显示 2.7,但是如何指定我想要 Python 3?还有其他我需要注意的问题吗?例如从记事本++运行脚本时?
我有一个脚本,它通过服务器的文本文件并报告 25% 和 10% 以下的可用磁盘空间。然而,有些服务器有一个 P: 驱动器,用于分页文件,因此我希望循环忽略这个驱动器。
这是循环部分:
# Start processing disk space reports against a list of servers
foreach($computer in $computers)
{
$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3" -EA SilentlyContinue
$computer = $computer.toupper()
foreach($disk in $disks)
{
$deviceID = $disk.DeviceID;
$volName = $disk.VolumeName;
[float]$size = $disk.Size;
[float]$freespace = $disk.FreeSpace;
$percentFree = [Math]::Round(($freespace / $size) * 100, 2);
$sizeGB = [Math]::Round($size / 1073741824, 2);
$freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
$usedSpaceGB = $sizeGB - $freeSpaceGB; …Run Code Online (Sandbox Code Playgroud) 如何使用包含日期的变量来充当 powershell 中的 get-date 函数?我有一个包含 2016-09-08 的变量 $date。我想从 $date 中减去一天。就像是:
$date = "2016-09-08"
$date.AddDays(-1)
Run Code Online (Sandbox Code Playgroud)
它不起作用。
我想使用 splatting 语法,但我想内联
所以而不是:
$p = @{Path = '.'}
ls @p
Run Code Online (Sandbox Code Playgroud)
我想要做
ls @@{Path = '.'}
Run Code Online (Sandbox Code Playgroud)
但这是一个语法错误。无论如何,是否可以在不必编写单独的变量的情况下生成哈希表?
我为什么要这样做?我更喜欢用于定义大量参数(例如 4 个或更多)的哈希表语法。但我宁愿不定义变量,我只想让我在哈希表定义的“顶部”调用的 cmdlet。
我的计算机上有一个批处理脚本,名为cs.bat. 当我进入cs命令提示符时,pushd将我带到某个目录并将我留在那里。在 PowerShell 中,该命令执行相同的操作,但会将我带回起始目录。
为什么会这样?在 Power Shell 中键入“cs”后,如何才能使其留在目录中?
我制作了一个打开某个程序的程序,然后在 x 时间后Ctrl+C它。
我现在正在使用这个[System.Windows.Forms.SendKeys]::SendWait("^{c}")。这个目标是某个窗口还是随机发送到当前窗口?
如何将其更改为某个窗口?
这是我的代码:
Write-Host "Safe Botting V0.1"
Write-Host "Initializing..."
Start-Sleep -s 3
Write-Host "Program started successfully with no errors."
While($true)
{
Write-Host "Starting bot..."
Start-Sleep -s 3
Start-Process -FilePath E:\Documents\bot.exe
Write-Host "Bot started successfully"
$rnd = Get-Random -Minimum 1800 -Maximum 10800
Write-Host "The bot will run for:"
Write-Host $rnd
Start-Sleep -s $rnd
Write-Host "Bot will now stop!"
[System.Windows.Forms.SendKeys]::SendWait("^{c}")
Write-Host "Bot terminated"
Write-Host "Starting cooldown time"
$rnb = Get-Random -Minimum 14400 -Maximum …Run Code Online (Sandbox Code Playgroud) 我试图在扫描文件夹时处理错误。假设我有类似的东西:
Get-ChildItem $somepath -Directory | ForEach-Object {
if(error occurred due to too long path) {
skip this folder then
} else {
Write-Host $_.BaseName
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我会打印文件夹,$somepath直到其中一个太长,然后循环停止。即使在使用SilentlyContinue. 即使到达太长的文件夹后,我也想打印。
当我运行此命令时:
$k = Get-Process | select Name -First 3
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
姓名 ---- AESTSr64 应用框架主机 支持服务
我想将每个结果放在一个单独的字符串类型变量中并获取其长度。我想知道如何访问返回的单个行。
我尝试了一个 foreach 如下:
foreach ($k in $three) {
write $k
}
Run Code Online (Sandbox Code Playgroud)
要得到
姓名 ---- AESTSr64 应用框架主机 支持服务
我只想输出没有标题和破折号的结果,并在同一行上显示每个字符串的长度(当我使用转换输入时,.ToString()我得到空字符串和零长度,因为每个结果都被视为一个对象)。
我试过这个,但它产生了错误的长度:
foreach ($k in $three) {
$t = $k | Out-String
write ("{0} {1}", $t, $t.Length)
}
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能产生这样的东西:
AESTSr64 08 应用框架主机 20 BtwRSupportService 18
编辑
使用以下答案和注释获得所需结果的一种方法:
$three = Get-Process | select -First 3 | ForEach-Object Name
foreach ($k in $three) {
[string]$c = "{0,-15} …Run Code Online (Sandbox Code Playgroud) 我想使用 PowerShell 来自动化 diskpart.exe,但 PowerShell 构建的脚本参数被视为无效。例如,下面的简单的 diskpart 脚本按预期工作:
$diskDetailsDPCom=@(
"select disk 0",
"detail disk",
"exit"
)
$diskDetailsDPCom | diskpart
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试通过连接变量或文字“0”来构建“select disk”命令时,diskpart 失败并显示The arguments specified for this command are not valid.:
$diskDetailsDPCom=@(
"select disk " + "0",
"detail disk",
"exit"
)
$diskDetailsDPCom | diskpart
Run Code Online (Sandbox Code Playgroud)
这里发生了什么,我该如何继续?
我正在尝试在 Powershell 中创建字典并将其传递给 C# 函数
$params= @{"abc"="123","xyz"=$False};
Run Code Online (Sandbox Code Playgroud)
C# 方法接受Dictionary<string,object>作为参数。
当我尝试MyFunc($params)从 PowerShell调用时,出现以下错误
无法转换参数“arguments”,值为“System.Collections.Hashtable”,“MyFunc”输入“System.Collections.Generic.Dictionary2[System.String,System.String]”:“无法创建类型的对象” System.Collections.Generic.Dictionary2[System.String,System.String]"