Get-Member为它包含的每种类型的对象返回一个对象名称和成员列表.
PS C:\src\t\getlast> $a = @(1,'now')
PS C:\src\t\getlast> $a | gm
TypeName: System.Int32
...
TypeName: System.String
...
Run Code Online (Sandbox Code Playgroud)
有没有办法获取数组对象本身的对象名称和成员列表?创建一个包含数组和不同类型的数组是我找到获取数组对象成员列表的唯一方法.
PS C:\src\t\getlast> $a = @(@(1),'now')
PS C:\src\t\getlast> $a.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\src\t\getlast> $a | gm
TypeName: System.Object[]
...
TypeName: System.String
...
Run Code Online (Sandbox Code Playgroud) 以下脚本从我提供的每台计算机成功获取制造商,型号,序列号和操作系统hostnames.txt.但是,它很慢,因为它必须连接到每台计算机上的WMI三次.
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
$BIOS = Get-WmiObject Win32_Bios -ComputerName $Computer
Run Code Online (Sandbox Code Playgroud)
使用PowerShell,如何连接到远程计算机的WMI一次并使用相同的连接执行三个查询?
$Array = @() ## Create Array to hold the Data
$Computers = Get-Content -Path .\hostnames.txt
foreach ($Computer in $Computers)
{
$Result = "" | Select HostPS,Mfg,Model,Serial,OS
$Result.HostPS = $Computer
$ErrorActionPreference = "SilentlyContinue" ## Don't output errors for offline computers
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
$BIOS = Get-WmiObject Win32_Bios -ComputerName $Computer
$ErrorActionPreference …Run Code Online (Sandbox Code Playgroud) $a会是“角落”或“特殊角”之类的东西,但可能会长到“标准窗户最多可打开37个”之类的东西,这意味着Substring()如果我无法使用简单的方法(无论如何,我都知道)想要查找中的前20个字符$a(这就是问题)。
我发现这是专为我想要的东西而设计的,但是它给了我
“ char [] ToCharArray(),char [] ToCharArray(int startIndex,int length)”
而且我不知道如何使其正常运行。
($a.ToCharArray | select -First 20) -join ""
Run Code Online (Sandbox Code Playgroud) 我正在通过API解析一些XML文本而不保存实际文件,并且当文本包含来自其他语言的字符时遇到问题.
当试图转换'ë'或其他类似的东西时,我最终得到了文本é.有没有办法在内存中更改变量的编码,因为我没有使用任何文件.
任何帮助将不胜感激.
下面的代码从list.txt文件中搜索400多个数字,以查看它是否存在于指定的文件夹路径中的任何文件中.
该脚本非常慢并且尚未完成,因为它在运行25分钟后没有完成.我们正在搜索的文件夹是507 MB(532,369,408字节),它包含1,119个文件和480个文件夹.任何有助于提高搜索速度和效率的帮助都非常感谢.
$searchWords = (gc 'C:\temp\list.txt') -split ','
$results = @()
Foreach ($sw in $searchWords)
{
$files = gci -path 'C:\Users\david.craven\Dropbox\Asset Tagging\_SJC Warehouse_\_Project Completed_\2018\A*' -filter "*$sw*" -recurse
foreach ($file in $files)
{
$object = New-Object System.Object
$object | Add-Member -Type NoteProperty –Name SearchWord –Value $sw
$object | Add-Member -Type NoteProperty –Name FoundFile –Value $file.FullName
$results += $object
}
}
$results | Export-Csv C:\temp\output.csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud) 我正在使用Rest API并收集了一块Json的粗糙块.我正在运行convertfrom-json来获取一个我想操作的powershell对象.基本上我需要修剪一些字段/值.
没有问题要"获取"我想要从对象中删除的字段,因为我可以深入到字段并收集容易的值,我被卡住的是如何从posh对象中删除该字段.非常感谢任何帮助.谢谢.
例:
$sample_json = @"
{
"fields": {
"field_one": 1,
"field_two": 2,
"field_three": "three",
"field_four": "remove_me",
"field_five": 5
}
}
"@
Clear-Host
$json_object = ConvertFrom-Json -InputObject $sample_json
$json_object
Run Code Online (Sandbox Code Playgroud)
得到:
fields
------
@{field_one=1; field_two=2; field_three=three; field_four=remove_me; field_five=5}
Run Code Online (Sandbox Code Playgroud)
所以问题是如何从$ json_object中删除"field_four"键及其值?抱歉,如果这很简单; 这几年我与Powershell有点脱节.
因此,例如,我有一个自定义对象$server。我想创建一个新属性,$server.output以包含大小不同的多行输出。最好的方法是什么?关于如何向对象添加简单的字符串属性,网络上似乎没有任何直接的信息?
仅供参考,我使用的是Powershell v4,但也可以为v5提供解决方案。
我有一段非常简单的代码,可以获取我需要的原始数据来计算任何wscript进程在过去30秒内使用的CPU的秒数
$prev=Get-Process | Where-Object { $_.Name -eq "wscript" }
$prev
start-sleep -Seconds 30
$curr=Get-Process | Where-Object { $_.Name -eq "wscript" }
echo "section 2"
$prev
echo "section 3"
$curr
Run Code Online (Sandbox Code Playgroud)
但是,在$ curr之后,$ prev中的值会重置,如下面的输出所示.第2节应与第1节相同,但它与第3节相匹配.
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
177 19 2640 9252 1,795.55 12308 1 wscript
177 19 2628 9340 1,799.67 17316 1 wscript
177 19 2652 9292 1,803.83 25248 1 wscript
section 2
177 19 2640 9252 1,825.28 12308 …Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的PowerShell数组:$ TestArray有2个文本列:Price和TimeStamp(这就是我得到它们的方式,没有什么可以做的):
Price TimeStamp
----- ----------------
0.0567 1542056680.72746
0.0567 1542056650.34414
0.0555 1542056197.46668
0.0551 1542056167.28967
Run Code Online (Sandbox Code Playgroud)
我想,在单个PowerShell行中添加一个舍入Time2值
$Time2 = [math]::Round($TestArray.TimeStamp)
Run Code Online (Sandbox Code Playgroud)
我想的代码是:
$TestArray | Add-Member -Name Time2 -MemberType NoteProperty -Value { [math]::Round($Table.TimeStamp) }
Run Code Online (Sandbox Code Playgroud)
当然,我可以做一个ForEach循环; 它可以很容易地处理它,但我想在这一行代码中实现这一点.
任何的想法 ?
干杯,菲利普
我创建了一个powershell gui,我想在我的windows.form中插入一个图标.
我这样做了,我用ps2exe生成了一个exe文件.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,230'
$Form.text = "Test"
$Form.TopMost = $false
$Icon = New-Object system.drawing.icon (".\icon\test.ico")
$Form.Icon = $Icon
Run Code Online (Sandbox Code Playgroud)
如果我带着我的exe带有图标test.ico的dir图标,但是现在我会在我的代码中加入图标,而不必带有我的exe的图标目录.
有可能吗?怎么样?
谢谢
powershell ×10
arrays ×1
encoding ×1
json ×1
performance ×1
pipeline ×1
properties ×1
substring ×1
wmi ×1