I have my LocalConfigurationManager set up as follows:
[DSCLocalConfigurationManager()]
Configuration LCMConfig
{
Node localhost
{
Settings
{
RebootNodeIfNeeded = $true
ActionAfterReboot = 'ContinueConfiguration'
AllowModuleOverwrite = $true
DebugMode = 'All'
}
}
}
LCMConfig
Set-DscLocalConfigurationManager -Path .\LCMConfig -Verbose -Force
Run Code Online (Sandbox Code Playgroud)
Then I kick off a DSC Configuration via
Start-DscConfiguration -Path .\RDS -Verbose -Wait
Run Code Online (Sandbox Code Playgroud)
This configuration sets up an RemoteApp server which involves a whole bunch of steps and several reboots. One of those reboots is happening in a loop over and over …
基本上,我想将命令 curl 转换为与当前在 linux 服务器中相同的参数,但在 Powershell 中,以便上传文件:
curl -v -T $file -u user:password http://myurl --cacert /opt/keystores/ca_cert.pem
Run Code Online (Sandbox Code Playgroud)
我找到了执行此任务的等效命令:PowerShell 3.0+ 的“Invoke-WebRequest”,但问题是我不知道如何使用 CA 证书文件 (.pem) 调用它,而且我还没有找到互联网上的任何样本。
谢谢!
我试过谷歌搜索不同的东西,但我似乎无法找到答案。
"ColumnOne","ColumnTwo","ColumnThree"
"C1V1","C2V1","C3V1"
"C1V2","C2V2","C3V2"
"C1V2","C2V2","C3V2"
"C1V3","C2V3","C3V3"
Run Code Online (Sandbox Code Playgroud)
$input = 'C:\Temp\test.csv'
$inputCsv = Import-Csv $input | Select-Object -Unique
$inputCsv | Export-Csv "$input-temp.csv" -NoTypeInformation
#Move-Item "$input-temp.csv" $input -Force
Run Code Online (Sandbox Code Playgroud)
"ColumnOne","ColumnTwo","ColumnThree"
"C1V2","C2V2","C3V2"
Run Code Online (Sandbox Code Playgroud)
"ColumnOne","ColumnTwo","ColumnThree"
"C1V1","C2V1","C3V1"
"C1V2","C2V2","C3V2"
"C1V3","C2V3","C3V3"
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我正在尝试创建一个 PowerShell 脚本,该脚本将在我的 Active Directory 中搜索密码将在 14 天后过期的用户,并向用户发送电子邮件提醒消息。在大多数情况下,我的脚本按预期工作,但我没有获得正确的电子邮件输出。
这是脚本:
Import-Module ActiveDirectory
$below = (Get-Date).AddDays(-77)
$above = (Get-Date).AddDays(-76)
$Persons = Get-ADUser -Filter {PasswordNeverExpires -eq $False -AND (PasswordLastSet -lt $above -AND PasswordLastSet -gt $below) -AND (LastLogonDate -like "*") -AND (MAil -like "*")}
foreach ($Person IN $Persons) {
$body = "Your Windows password will expire in 14 days. Please consider changing your password. - $User.Name - $User.PasswordLastSet"
Send-MailMessage -To $User.Mail -Subject "Alert! Password Expiring Soon!" -From 'Do_Not_Reply@domain.com' -Body $body -SmtpServer smtp.server.com
}
Run Code Online (Sandbox Code Playgroud)
这就是我在它发送的邮件正文中得到的内容。 …
我有一个 PowerShell .ps1 文件,其中包含脚本顶部的函数,后跟调用这些函数的不同命令。我正在使用 Pester 对我的脚本文件进行单元测试。
如何模拟 PowerShell .ps1 脚本中的函数?
我曾尝试模拟该函数,但收到一条错误消息,提示“找不到命令”。
我还尝试在描述块中添加一个空的“虚拟”函数。这不会给我上述错误,但它没有正确地模拟脚本中的函数。
我有两个文件。一个保存测试,另一个保存函数和对函数的调用。下面是两个例子:
Function Backup-Directory([switch]$IsError)
{
If($IsError)
{
Write-Error "Error"
Exit 1
}
}
Backup-Directory $true
Run Code Online (Sandbox Code Playgroud)
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path) -replace '\\test', '\main'
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
$productionFile = "$here\$sut"
Describe "File1" {
Context "When the Back-Directory outputs an error." {
# Arrange
Mock Back-Directory { }
Mock Write-Error
# Act
& $productionFile
$hasSucceeded = $?
# Assert
It "Calls Backup-Directory" {
Assert-MockCalled …Run Code Online (Sandbox Code Playgroud) 我在 Powershell 中有一组自定义对象。每个对象都有一个可以在数组中查找的唯一引用和一个我希望稍后使用的值,该值不一定是唯一的。该数组填充在 for 循环中,如下所示
#Create empty array
$array1 = @()
#Populate array
foreach($otherElement in $otherArray){
#Create custom array element
$arrayElement = New-Object PSObject
#Update Credential element
$arrayElement | Add-Member -type NoteProperty -Name 'objRef' -Value $($otherElement.getAttribute("ref") )
$arrayElement | Add-Member -type NoteProperty -Name 'objValue' -Value $($otherElement.getAttribute("someValue") )
#Add element to array
$array1 += $arrayElement
}
Run Code Online (Sandbox Code Playgroud)
构建数组后,我想以某种方式访问与正确objRef对应的objValue。我知道您可以使用-contains参数测试数组是否包含一个值,但我不知道如何使用该值获取对象的索引。
基本上这个数组就像一个查找表。我想要一个函数来放入 objRef 以获取 objValue。
在这种情况下 objValue 是一个System.Management.Automation.PSCredential,为了安全起见,每个对象的密码都是在运行时输入的。在工作中,我有时必须使用相同的 5 个凭据在不同的机器上安装相同的软件 30 次,弄清楚这一点将帮助我远程自动化该过程。
提前致谢!
如何将 PSObject 实例解析为 C# POCO模型实体?
PSObject 实例是一个包含以下属性的动态对象:
@{Name=David; Diff=0.0268161397964839; File=Output.txt}
Run Code Online (Sandbox Code Playgroud)
我有适合这些领域的 C# POCO 模型。
有没有好的投射方式?
我收到Get-ChildItem : *A parameter cannot be found that matches parameter name 'Directory'*错误消息,下面是使用的脚本,请帮助我找到问题?
脚本:
Function Clear-ReadOnlyFiles([string]$path)
{
"Clearing readonly attribute from files in $path"
New-Variable -Name read_only -Value 1 -Option readonly
Get-ChildItem -Path $path |
Where-Object { $_.attributes -match 'readonly' } |
ForEach-Object {
$_.attributes = $_.attributes -Bxor $read_only }
}#end Clear-ReadonlyFiles
$ZipCmd = "{0}\7za.exe" -f $BackupDir
$ZipCmdArgs = "-sdel"
# Grab Directories in Location
$Directories = Get-ChildItem -Path $BackupDir -Directory
# Cycle Through and Launch 7za to Compress …Run Code Online (Sandbox Code Playgroud) 我有 250 个 VM,用于安装了不同版本的 SQL 服务器的不同客户端。
我运行了下面的命令来获取详细信息,但信息没有成功
SELECT
SERVERPROPERTY('productversion'),
SERVERPROPERTY('productlevel'),
SERVERPROPERTY ('edition'),
@@version,
SERVERPROPERTY('PatchLevel')
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?我需要有关 SQL Server 版本、服务包、累积更新和安装到服务器的补丁等详细信息的信息。
所以我写了一系列的函数并将它们插入到一个 PS 模块 (.psm1) 中。其中之一是一个简单的 ErrorAndExit 函数,它将消息写入 STDERR,然后调用exit(1);,这是一种允许轻松重定向错误消息的习惯。在调试我的脚本时,在普通的 PowerShell 或 ISE 中,如果我调用一个函数,该函数又调用 ErrorAndExit,它不仅会退出脚本,还会退出整个 PowerShell 进程。终端和 ISE 都会立即死亡。终端,我可能只是理解,但是ISE?!最令人沮丧的部分当然是在窗口消失之前我看不到错误消息。
我相信这与我的调试方式有关 - 我已经定义了很多发生在链中的函数,并且已经注释掉了启动链的调用。我正在导入脚本并从提示中调用函数。由于脚本是为自动化设计的,在实际使用中杀掉整个PS进程不会有问题,但我需要看看调试输出是什么。
有问题的函数,在 Common.psm1 中:
function errorAndExit([string]$message)
{
logError($message);
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
凡logError通过$message对Write-Error。此调用导致 PS 或 ISE 终止的示例函数:
function branch([string]$branchName, [int]$revision, [string]$jenkinsURL, [switch]$incrementTrunk)
{
Set-Variable -Name ErrorActionPreference -Value Stop;
log("Script starting, parameters branchName=$branchName, revision=$revision, jenkinsURL=$jenkinsURL");
if (-not ($branchName -match "\d{4}\.\d{2}\.\d"))
{
errorAndExit("Provided branch name $branchName is not a valid YYYY.MM.R string");
}
...
Run Code Online (Sandbox Code Playgroud)
我的 Common.psm1 模块正在使用一个简单的 …
powershell ×10
arrays ×1
c# ×1
curl ×1
dsc ×1
exit ×1
pester ×1
psobject ×1
sql ×1
sql-server ×1
unit-testing ×1
windows ×1