我一定在这里遗漏了一些基本的东西,但我是 powershell 的新手......
我写了一个函数并将其保存在一个名为“UserSelectionList.psm1”的文件中,该函数是这样的:
function Global:UserSelectionList([String[]] $UserOptions)
{
...
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试用这个脚本调用它:
Import-module "l:\support downstream\solarc\cngl\powershell scripts\userselectionlist.psm1"
$Options = "a","b","c"
cls
$result = UserSelectionList $Options
echo $result
Run Code Online (Sandbox Code Playgroud)
由此产生的错误是:
The term 'UserSelectionList' 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:5 char:28
+ $result = UserSelectionList <<<< $Options
+ CategoryInfo : ObjectNotFound: (UserSelectionList:String) [], …Run Code Online (Sandbox Code Playgroud) 执行 Get-ADObject 查询后,我得到了两个 PSObjects(用于 2 个单独的 OU),我们称它们为 $query1 和 $query2,它们包含类型为 Member-Objects
Selected.Microsoft.ActiveDirectory.Management.ADObject
Run Code Online (Sandbox Code Playgroud)
现在我想将这两个 PSObjects,$query1 和 $query2,合并到一个包含两个查询的 ADObjects 的单个 PSObject 中,但我真的不知道如何做这样的事情。
如何让以下文本的输出仅显示引号中的文本(不带引号)?
示例文本”
this is an "apple". it is red
this is an "orange". it is orange
this is an "blood orange". it is reddish
Run Code Online (Sandbox Code Playgroud)
变成:
apple
orange
blood orange
Run Code Online (Sandbox Code Playgroud)
理想情况下,如果可能的话,我想在一个班轮中进行。我认为这是带有 -match 的正则表达式,但我不确定。
我正在使用以下脚本文件 (ftp_test_2.ps1) 将文件传输到 ftp 服务器:
#we specify the directory where all files that we want to upload are contained
$Dir="C:/Users/you-who/Documents/textfiles/"
#ftp server
$ftp = "ftp://192.168.100.101:21/"
$user = "aP_RDB"
$pass = "pw"
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
#list every txt file
foreach($item in (dir $Dir "*.txt"))
{
"creating URI..."
$uri = New-Object System.Uri($ftp+$item.Name)
$uri
"attempting upload... $item"
$webclient.UploadFile($uri, $item.FullName)
}
Run Code Online (Sandbox Code Playgroud)
当 $ftp 设置为本地主机并连接到另一个 ftp 测试服务器时,这可以正常工作。但是,在生产 ftp 服务器上,服务器拒绝上传并显示 Powershell 错误:
Exception calling "UploadFile" with "2" argument(s): "The remote server …Run Code Online (Sandbox Code Playgroud) 我有大型 CSV 文件(每个 50-500 MB)。在这些上运行复杂的 power shell 命令需要永远和/或遇到内存问题。
处理数据需要按公共字段分组,比如在 ColumnA 中。因此,假设数据已经按该列排序,如果我随机拆分这些文件(即每个 x 千行),那么匹配的条目仍可能以不同的部分结束。A 中有数千个不同的组,因此将每个组拆分为一个文件会创建多个文件。
如何将其拆分为 10,000 行的文件而不丢失组?例如,第 1-13 行将是 A 列中的 A1,第 14-17 行将是 A2 等,第 9997-10012 行将是 A784。在这种情况下,我希望第一个文件包含第 1-10012 行,下一个以第 10013 行开头。
显然我想保留整行(而不仅仅是 A 列),所以如果我将所有结果文件粘贴在一起,这将与原始文件相同。
我有一个 PowerShell 函数,它接受一个可选参数,使用 a 进行验证ValidateSetAttribute,并根据该值添加另一个动态参数。但是,在严格模式下,当尝试访问DynamicParam块内部的参数时,并且我没有明确设置所述参数,然后我收到一个错误,指出该变量未定义。
Param(
[Parameter()]
[ValidateSet('A', 'B')]
[string] $Target = 'A'
)
DynamicParam {
if ($Target -eq 'B') { # <- Here it fails
# Add new parameter here...
}
}
end {
Write-Host $Target
}
Run Code Online (Sandbox Code Playgroud)
该脚本在使用A或B作为第一个参数调用时有效,但在省略该参数时失败。有趣的是,如果我从参数定义中删除 theParameterAttribute或 the ValidateSetAttribute,它就会起作用。
我当前的解决方法是使用访问变量$PSBoundParameters并检查是否设置了参数,如下所示:
if ($PSBoundParameters.ContainsKey('Target') -and $PSBoundParameters.Target -eq 'B') {
# Add new parameter here...
}
Run Code Online (Sandbox Code Playgroud)
虽然这工作正常,但如果我想检查该值,它有一个缺点A:作为A参数的默认值,$PSBoundParameters当省略参数并应用默认值时,它不会被添加到。所以我需要修改我的支票以明确检查: …
我正在尝试将输出日志通过管道传输到另一个脚本进行处理。但是,如果我正确理解这个问题,powershell 会等待第一个命令完成,然后再将输出发送到第二个命令。
例如,这按预期工作(脚本收到“hi”),因为echo很快完成:
echo "hi" | python script.py
Run Code Online (Sandbox Code Playgroud)
而在这个(caddy是网络服务器,所以它没有完成)中,球童的标准输出永远不会到达脚本的标准输入:
caddy run | python script.py
Run Code Online (Sandbox Code Playgroud)
是否可以将第一个命令的输出异步转发到第二个命令,而无需等待第一个命令完成?
--
编辑:最小的例子
echo "hi" | python script.py
Run Code Online (Sandbox Code Playgroud)
caddy run | python script.py
Run Code Online (Sandbox Code Playgroud)
# output.py
from time import sleep
while True:
print("SENDING")
sleep(1)
Run Code Online (Sandbox Code Playgroud)
这有效(打印到 stdout Received: SENDING):
python onetime.py | python input.py
Run Code Online (Sandbox Code Playgroud)
这不起作用(没有打印任何内容):
python output.py | python input.py
Run Code Online (Sandbox Code Playgroud) 我正在构建一个脚本来修复 SQLite 数据库。其中一个字段包含一个相当大的字符串,其中包含 JSON 结构。
在结构内部,有一个像这样构建的“项目”数组:
...
},
"items": [
{
"id": 001,
"Model": {
"modelname": "Model1"
},
...
"location": null,
...
},
{
"id": 002,
"Model": {
"modelname": "Model2"
},
...
"location": "1020",
...
},
{
"id": 003,
"Model": {
"modelname": "Model3"
},
...
"location": null,
...
},
...
],
Run Code Online (Sandbox Code Playgroud)
我需要的是查找带有"location": null和不带有的所有项目"modelname": "Model3"(因此,在本示例中,我将仅获取带有 的项目"id": 001),然后迭代它们以编辑“location”属性。
我并不是真正的 PowerShell 专家,所以我不知道我可以获取和使用哪些对象或对象数组。
我怎样才能找到我需要的项目并迭代所有这些项目?
自从将我们的构建代理从 PowerShell 7.2.11 升级到 7.2.12 以来,他们一直报告以下错误:
Cannot bind parameter 'AccessToken'. Cannot convert the *** value of type "System.String" to type "System.Security.SecureString".
Run Code Online (Sandbox Code Playgroud)
我们的脚本如下:
Cannot bind parameter 'AccessToken'. Cannot convert the *** value of type "System.String" to type "System.Security.SecureString".
Run Code Online (Sandbox Code Playgroud)
这之前是有效的,回滚到我们之前的构建代理映像已经解决了这个问题。
这可能是一个愚蠢的问题,所以请友善,哈哈。我正试图集中精力思考我刚刚遇到的事情。
第一的
我有一个可以使用以下内容枚举的哈希表:
$ht = [hashtable]@{"Key1"="Value1";"Key2"="Value2"}
$ht.GetEnumerator()
Name Value
---- -----
Key1 Value1
Key2 Value2
Run Code Online (Sandbox Code Playgroud)
如果我将其存储到一个变量中,则它仅在该变量的一次调用中存在。
$KeyPairs = $ht.GetEnumerator()
$KeyPairs
Name Value
---- -----
Key1 Value1
Key2 Value2
$KeyPairs
# Nothing is returned as if $KeyPairs lost its value
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我理解为什么会这样吗?
第二
通常,对于集合,我有时想要针对单个项目进行测试(例如查看一个实例的属性),我可以使用 Select-Object 或通过索引:
$array = @("Value1","Value2")
$array | select -first 1
Value1
$array[0]
Value1
Run Code Online (Sandbox Code Playgroud)
哈希表枚举器似乎只支持 Select-Option,而不支持索引。
$ht = [hashtable]@{"Key1"="Value1";"Key2"="Value2"}
$ht.GetEnumerator() | Select -first 1
$ht.GetEnumerator() | Select -first 1
Name Value
---- -----
Key1 Value1
($ht.GetEnumerator() | measure).count
2 …Run Code Online (Sandbox Code Playgroud)