基本上,我设置了一个变量,我想检查文件是否存在。如果没有,则需要创建。我觉得我在变量或字符串值方面遇到麻烦......我不太确定。无论如何,它曾经工作过一次!在创建之后,我通过删除文件来测试它。该脚本从未提示我重新创建文件...
# Setting up a few variables
$SysAdmin = "MyDomain\MyUser"
$StudentID = Read-Host "Enter the Student ID you wish to look up."
$ADDC = "AD04"
$HashedPassword = "$env:USERPROFILE\OneDrive\tier-2_pass.txt"
# Checking for a text file with a hashed password and creating it if it's not found.
If (-Not (Test-Path $HashedPassword)) {
$SecretPassword = Read-Host -AsSecureString "Enter a secret password"
$SecretPassword | ConvertFrom-SecureString | Out-File "$HashedPassword"
}
# Getting the hashed password from the text file.
$Password = Get-Content $HashedPassword | ConvertTo-SecureString
$Creds = New-Object System.Management.Automation.PSCredential $SysAdmin,$Password
Invoke-Command -ComputerName $ADDC -Credential $Creds { Get-ADUser -Properties * -Filter { Pager -eq $Using:StudentID } | Select-Object -Property Name, DisplayName, SamAccountName, @{Name = "Student ID"; Expression = {$_.Pager}}, @{Name = "Possible Location-1"; Expression = {$_.Office}}, @{Name = "Possible Location-2"; Expression = {$_.PhysicalDeliveryOfficeName}} | FL }
所以,这就是脚本....我看到没有错误,除非我对Get-Content
变量做$HashedPassword
...此时它让我知道文件确实消失了。我错过了什么?