You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用New-Item和Add-Content创建PowerShell脚本文件遇问题求助

问题:PowerShell生成计划任务脚本时变量解析异常

这是我首次在SO(及任何公开平台)发帖,若问题已被解答或格式不当,敬请谅解。我是PowerShell脚本编写新手,希望完成以下需求:编写脚本收集操作系统用户凭据、密码及远程服务器的IP/FQDN/主机名,生成供计划任务调用的ps1文件,实现每日将指定文件夹复制到远程服务器并覆盖目标文件夹。但在使用New-Item和Add-Content cmdlets添加代码行时遇到问题。

当前脚本

#Clear the Screen
Clear-Host

#Create Variable for OS User Account
$User = read-host "What is the OS User that has permissions to the remote web server?"

#Clear the Screen
Clear-Host

#Password for OS User Account
$Password = Read-Host "What is the password of the OS account that has permissions to the remote web server?"

#Clear the Screen
Clear-Host

#Convert Password String
$PWord = ConvertTo-SecureString -String $Password -AsPlainText -Force

#Combine User Name and Password into One Entity
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

#IP Address of Destination ICA Web Server
$DestServer = Read-Host "What is the IP address\FQDN\Hostname of the Destination Web Server"

#Create a PowerShell File that will be used for Task Scheduler
New-Item "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" -ItemType File -Force -Value "$PWord = ConvertTo-SecureString -String $Password -AsPlainText -Force"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "$Session = New-PSSession -ComputerName $DestServer -Credential $Credential"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "Copy-Item `"C:\Web\Web\Download`" -Destination `"C:\Web\Web\`" -ToSession $Session -Recurse -Force"

生成的异常文件内容

使用TestUser作为用户名、PW作为密码、10.10.10.10作为远程服务器时,生成的ps1文件内容如下:

System.Security.SecureString = ConvertTo-SecureString -String PW -AsPlainText -Force
System.Management.Automation.PSCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList TestUser, System.Security.SecureString

 = New-PSSession -ComputerName 10.10.10.10 -Credential System.Management.Automation.PSCredential
Copy-Item "C:\Web\Web\Download" -Destination "C:\Web\Web\" -ToSession  -Recurse -Force

问题详情

  • 第一行显示System.Security.SecureString而非$PWord变量;
  • 第二行显示System.Management.Automation.PSCredential和System.Security.SecureString而非$Credential和$PWord变量;
  • 第三行未显示$Session变量,且Credential参数值为System.Management.Automation.PSCredential;
  • 第四行未显示$Session变量。

似乎PowerShell不支持通过New-Item和Add-Content直接添加变量,恳请各位提供建议!


更新:修正后的可用脚本

感谢mklement0提供解决方案,以下是修正后的可用脚本:

#Clear the Screen
Clear-Host

#Create Variable for OS User Account
$User = read-host "What is the OS user account that has permissions to the remote web server?"

#Clear the Screen
Clear-Host

#Password for OS User Account
$Password = Read-Host "What is the password of the OS user account that has permissions to the remote web server?"

#Clear the Screen
Clear-Host

#Convert Password String
$PWord = ConvertTo-SecureString -String $Password -AsPlainText -Force

#Combine User Name and Password into One Entity
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

#IP Address of Destination ICA Web Server
$DestServer = Read-Host "What is the IP address, FQDN, or Hostname of the remote web server"

#Create a PowerShell File that will be used for Task Scheduler
New-Item "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" -ItemType File -Force -Value "`$OSUser = `"$User`""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "`$PWord = ConvertTo-SecureString -String `"$Password`" -AsPlainText -Force"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "`$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList `$OSUser, `$PWord"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" ""
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "`$Session = New-PSSession -ComputerName `"$DestServer`" -Credential `$Credential"
Add-Content "C:\Test\ScheduledTask\CopyPasteDownLoadFolder.ps1" "Copy-Item `"C:\Web\Web\Download`" -Destination `"C:\Web\Web\`" -ToSession `$Session -Recurse -Force"

内容的提问来源于stack exchange,提问作者SeveredTRUTH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 19:26:28