如何将一些数据从参数传递到哈希表数组?
filnemane.ps1 -param @{ a = 1, b = 2, c =3}, @{ a = 4, b = 5, c =6}
Run Code Online (Sandbox Code Playgroud)
到这个输出:
$param = @(
@{
a=1,
b=2,
c=3
},
@{
a=4,
b=5,
c=6
}
)
Run Code Online (Sandbox Code Playgroud)
谢谢。
我试图在类中的函数中运行进程,但它什么也不做。看起来编译器忽略了它,但是,在类之外它完美地工作。
<# Works here #>
#Get-Process
class Test
{
[void]TestFunction()
{
<# DOES NOT WORK HERE #>
Get-Process
}
}
[Test]$object = [Test]::new()
$object.TestFunction()
<# Works here #>
#Get-Process
Run Code Online (Sandbox Code Playgroud)
PS 我在 macOS 上使用 PowerShell 和 VS Code
我正在处理带有 3 个文件的 GUI 的 PowerShell 脚本:
window1.ps1 - 输入变量并开始 start.vbsstart.vbs - 开始 window2.ps1window2.ps1 - 显示在 window1 中输入的值在窗口1,我进入value1到textbox1和点击button。
通过单击它,value1应该存储到一个global variable,以便window2可以弹出(通过 VBS)并使用value1.
为代码button在window1:
$button_OnClick=
{
$global:value1 = $textBox1.Text
start-process .\start.vbs
}
Run Code Online (Sandbox Code Playgroud)
开始.vbs:
Set objShell = CreateObject("Wscript.Shell")
objShell.run("powershell.exe -nologo -file .\window2.ps1"), 0, True
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好。据我了解,value1是一个全局变量,可以在window2.ps1.
window2一个只有一个文本的空窗口也是如此value1。
$label1.Text = "$value1"
Run Code Online (Sandbox Code Playgroud)
但是由于某种原因,该变量为空且window2为空。
我错过了什么?我仍在学习 PowerShell,我可能会遗漏一个细节。
注意:我知道我 …
我正在为一个相当复杂的脚本编写测试,脚本中有一个特定的函数可以向用户输出不同系列的日志消息。我想断言是否正在显示特定的日志消息。
主要问题是我不知道什么参数隐式处理我传递Write-Hostcmdlet的文本。
这是一些代码,它们捕捉了我正在尝试做的事情的前提......
function TestFunction($TestInput) {
if ($TestInput -contains 1) {
Write-Host "TestInput contains a 1"
}
if ($TestInput -contains 3 ) {
Write-Host "TestInput contains a 3"
}
if ($TestInput -contains 4 ) {
Write-Host "TestInput contains a 4"
}
}
Run Code Online (Sandbox Code Playgroud)
Describe "TestFunction" {
It "should call Write-Host with appropriate message if 1 or 3 is passed" {
Mock Write-Host {}
TestFunction -TestInput @(1, 2, 3)
Assert-MockCalled Write-Host -Exactly 2 -Scope It
Assert-MockCalled Write-Host -Exactly …Run Code Online (Sandbox Code Playgroud) 我有一个 PS 脚本,我用它逐行读取一个 csv 文件到一个变量中:
CSV :
header1;header2
column1;column2
Run Code Online (Sandbox Code Playgroud)
PS脚本:
#-------------------------
# Func declarations
#-------------------------
function myFunc{
param(
[Parameter(Mandatory=$true)][string]$param1,
[Parameter(Mandatory=$true)][string]$param2
)
#Do something
}
#-------------------------
# Run
#-------------------------
$lines = Import-Csv .\etc\file.csv -Delimiter ";" -Header "header1","header2"
foreach($line in $lines) {
myFunc -param1 $line.header1 -param2 $line.header1
}
Run Code Online (Sandbox Code Playgroud)
电话
myFunc -param1 $line.header1抛出一个:
无法将值转换为 System.String (...) ParameterBindingArgumentTransformationException
检查 $line.header1(或 $line.header2)的类型$line.header1.GetType()并按预期返回System.string。
[Parameter(Mandatory=$true)]$param1第一个上的类型定义 :
,第一个被识别,但第二个为空...为什么?
我正在获取我需要的信息,尽管在导出信息时出现错误。
当我运行此代码时:
get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Export-csv : Access to the path 'C:\export.csv' is denied.
At line:1 char:14
+ get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
如何导出为 CSV?
目前我可以使用 Beyond Compare 脚本将结果输出到文件。你们都能够帮助我弄清楚如何通过 Powershell 检查结果文本文件中的差异。不过,我还有一个问题。要将结果存入文本文件,Beyond Compare 让您使用一个基本脚本,然后通过 cmd 运行该脚本。我的问题是如何通过 Powershell 而不是 cmd 运行脚本?下面是 cmd 中用于运行脚本的命令。
c:\Program Files\Beyond Compare 4>bcompare.exe @"C:\script.txt" "C:\test1" "C:\test" "C:\bcreport\report.txt"
Run Code Online (Sandbox Code Playgroud)
它引用的脚本如下。
load "%1" "%2"
expand all
folder-report layout:side-by-side options:display-mismatches output-to:"%3"
Run Code Online (Sandbox Code Playgroud)
感谢您提供任何意见。让它工作是我正在处理的更大的 Powershell 脚本的一部分。如果我可以让它从 Powershell 运行,它将为我节省大量时间,因为我将能够编写需要数小时的脚本。
我有一个 CSV 格式的用户列表,但我需要按广告中的名称从每个用户那里收集 SamAccount 属性。
CSV模型
脚本
Get-ADObject -Filter 'ObjectClass -eq "user" -and userAccountControl -eq "512"' -Properties * | Select-Object SamAccountName,CN,DisplayName, | Export-CSV -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)
我有点迷茫我不知道如何使用名称进行 foreach
我正在尝试但没有成功。
试图根据 csv 文件上的名称获取 samaccountname。
Import-Csv -Path C:\Temp\userteste.csv | foreach-Object {Get-ADUser -Filter {Name -like $_.name} -Properties Name | Select-Object samAccountName}
Run Code Online (Sandbox Code Playgroud)
并导出到 csv 文件。
我的目的是配置一个私有源,以便能够在Azure Pipelines 中使用我的PowerShell模块。
我已经一步一步地遵循了这个Microsoft指南:https : //docs.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops。
以下代码(从我的开发机器执行)证明提要和包都处于有效状态:
$patToken = "**************************************" | ConvertTo-SecureString -AsPlainText -Force
$credsAzureDevopsServices = New-Object System.Management.Automation.PSCredential("giacomo.stelluti@dev4side.com", $patToken)
Get-PSRepository
Find-Module -Repository PowershellAzureDevopsServices -Credential $credsAzureDevopsServices
Install-Module -Name Get-Hello -Repository PowershellAzureDevopsServices -Credential $credsAzureDevopsServices
Run Code Online (Sandbox Code Playgroud)
这是输出:
Name InstallationPolicy SourceLocation
---- ------------------ --------------
PSGallery Untrusted https://www.powershellgallery.com/api/v2
PowershellAzureDevopsS... Trusted https://pkgs.dev.azure.com/dev4side/Dev4side%20-%20Intranet.ai/_packaging/iai-feed/nuget/v2
Name : Get-Hello
Version : 1.0.0
Type : Module
Description : Package description
Author : dev4side
CompanyName :
Copyright : Copyright 2020 Dev4Side SRL …Run Code Online (Sandbox Code Playgroud) 我正在使用 PowerShell 在 Azure DevOps 中创建工作项。
该项目已创建,并且 PowerShell 中显示了一个新的项目 ID:
如何获取此 ID 以在下一个命令中使用以更新此工作项?
谢谢!
powershell ×10
azure ×2
azure-devops ×1
class ×1
export ×1
export-csv ×1
macos ×1
nuget ×1
pester ×1
vbscript ×1
winforms ×1