如何将 Powershell 脚本的工作目录设置为与脚本存在相同的目录,以便无论您从哪个目录调用它,它都会始终在脚本的根目录中查找文件?
例如,如果您按如下方式调用 foo.ps1:
c:> d:\MyScript\foo.ps1
Run Code Online (Sandbox Code Playgroud)
foo.ps1 会将工作目录设置为 d:\MyScript\,这样当它 - 例如 - 查找配置文件时,它不会在 C:\ 中搜索它,而是在 d:\MyScript\
我试图在 PowerShell 中找到一些灵活更改/替换管道元素的方法:
Function Where-DirectlyReportsTo {
Param (
[Parameter(
ValueFromPipeline = $true,
HelpMessage = "The ADUser object to be tested"
)]
[Microsoft.ActiveDirectory.Management.ADUser] $ADUser,
[Parameter(
Mandatory = $true,
ValueFromPipeline = $false,
Position = 0
)]
[String] $mgrDN
)
Process {
If ($ADUser) {
If ($ADUser.Manager -eq $mgrDN) { Return $ADUser }
}
}
}
$Properties = @("Manager")
$users = Get-ADUser -Filter * -SearchBase $OU -Properties $Properties
[ScriptBlock] $sb = {Where-DirectlyReportsTo "CN=Colonel Foobar,$OU"}
$DNs = $users | $sb | %{$_.DistinguishedName}
Run Code Online (Sandbox Code Playgroud)
我想返回向 Foobar …
我试图找出调用需要日期范围参数(例如:20130801-20130815)的 exe 的最佳方法,然后循环它,使其减去 15 天并使用新的日期范围调用 exe。
我想过使用 do until 但我不确定如何(powershell/编程新手)但我确定这远非正确的方法:)。我刚刚开始弄清楚这一点,所以提前感谢您的任何/所有帮助。
do {
$startDate = (Get-Date).adddays(-34)
$requireddate = some date that is set ad-hoc
$startdate.ToString("yyyyMMdd")
#[datetime]::parseexact($startdate,"MMddyyyy",$null)
Call THE EXE at this point with the parameters $startdate and $enddate
$enddate = $startdate.AddDays(-15)
write-host $enddate.ToString("yyyyMMdd")
}
until ($enddate -eq $requireddate)
Run Code Online (Sandbox Code Playgroud) 我的脚本一直困扰着我,但出现以下异常
copy-item : Cannot find drive. A drive with the name 'F' does not exist.
At C:\Program Files (x86)\CA\ARCserve Backup\Templates\RB_Pre_Process.ps1:58 char:1
+ copy-item -Path $drive -Destination $DST_DRIVE -Recurse -ErrorAction Stop
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (F:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Run Code Online (Sandbox Code Playgroud)
这就是我的脚本的样子。我正在驱动器 F: 上安装 ISO 映像,并且我添加了一个“start-slepp -s 5”命令,以便我可以验证映像是否已安装,确实如此!
$BACKUP_PATH = "E:\00_BACKUP_DATA"
$DR_PATH = "E:\01_DR_DATA"
$ISO_IMAGE = "C:\Program Files (x86)\CA\ARCserve Backup\Templates\Winpe_x64.iso"
$DST_DRIVE = "E:\"
try {
New-EventLog -LogName Application -Source "RB_Pre_Process.ps1" -ErrorAction Stop
} catch [System.InvalidOperationException] {
Write-host $_ …Run Code Online (Sandbox Code Playgroud) 我有 powershell 文件,其中我有如下变量 decalration 行
[string] $global:myExePath = "\\myshare\code\scripts";
Run Code Online (Sandbox Code Playgroud)
我想更换 \\myshare\code\scripts与\\mynewshare\code1\psscript在运行时通过执行PowerShell脚本。
我在用
Get-Content $originalfile | ForEach-Object { $_ -replace "\\myshare\code\scripts", $mynewcodelocation.FullName } | Set-Content ($originalfile)
如果我正在执行
{ $_ -replace "scripts", $mynewcodelocation.FullName }它工作正常,但它不工作{ $_ -replace "\\myshare\code\scripts", $mynewcodelocation.FullName }
这里有什么问题?
例如,我想运行位于 c:/cygwin/ 目录中的 cygwin.bat...
我尝试了以下操作,但出现错误:
cd c:/cygwin ./cygwin.bat
我正在尝试从函数返回 $true 或 $false ,并得到一个 array 。如果我删除 listBox 消息,它会按预期工作。有谁知道为什么?
function TestEmptyFields()
{
$empty= $false
$listBox1.Items.Add("Testing fields")
if ($txtPrtName.get_text()-eq "")
{
$listBox1.Items.Add("Empty name")
$empty= $true
}
elseif ($txtPrtIP.get_text() -eq "")
{
$listBox1.Items.Add("Empty Ip")
$empty= $true
}
else
{
$empty= $false
}
$listBox1.Items.Add($txtPrtName.get_text())
$listBox1.Items.Add($txtPrtIP.get_text())
return $empty
}
Run Code Online (Sandbox Code Playgroud)
但它像这样工作正常:
function TestEmptyFields()
{
if($txtPrtName.get_text()-eq "")
{
return $true
}
elseif ($txtPrtIP.get_text() -eq "")
{
return $true
}
else
{
return $false
}
}
Run Code Online (Sandbox Code Playgroud) 我有下面的静默卸载/等待/安装脚本,我需要将其推送给用户,但我需要编写脚本,以便它以管理员身份运行,我找到了一些脚本,但我不明白如何编写脚本,任何帮助被赞赏。另外,我是否必须将管理员脚本放入两次?(即在卸载脚本的第一行然后在第二个安装脚本的行之前)还是在我运行它时只出现一次?
Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -eq "On-Screen Takeoff"} | foreach-
object -process {$_.Uninstall()}
Start-Sleep -Seconds 25
$arguments="/quiet"
Start-Process "\\davisconstruction.com\ROOT\Installs\OnCenter\OST\Testverion3906\ost3906.msi" $arguments
Run Code Online (Sandbox Code Playgroud) 我已经完成了我想要发送邮件的命令Powershell。这是我的代码
powershell.exe
$user="username@gamil.com"
$pass=cat I:\password.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $pass
send-MailMessage -SmtpServer smtp.gmail.com -Credential $mycred -Usessl true -From 'username@gamil.com' -To 'usernametwo@gamil.com' -Subject 'failure Test'
Run Code Online (Sandbox Code Playgroud)
当我在命令提示符下执行时,上面的代码工作正常,但当我尝试制作.bat文件时却没有。我用代码做了什么问题?
powershell ×10
.net ×1
batch-file ×1
command ×1
copy-item ×1
exception ×1
pipe ×1
scriptblock ×1
smtp ×1