我遇到一个问题,当我尝试在我的域中的远程计算机上创建计划任务时,出现访问被拒绝错误。奇怪的是,当我在Powershell中使用“新建远程Powershell选项卡”按钮并运行代码时,它可以完美运行。但是我似乎无法通过正常运行powershell脚本来复制它。我具有用于与远程计算机创建会话的域管理员凭据,但这似乎无关紧要。使用远程Powershell选项时,是否可以复制我似乎获得的权限?
function Install {
$hostname = Read-Host -Prompt "Enter hostname"
echo 'Testing connection...'
If(!(Test-Connection -ComputerName $hostname -Count 1 -quiet)){
echo "`n"
echo 'There was an issue connecting to this computer.'
pause
Install
}
echo 'Connection successful!'
Get-Service -Name WinRM -ComputerName $hostname | Start-Service
$cd = Convert-Path .
Copy-Item -Path "$cd\Install.bat" -Destination "\\$hostname\C$\Install.bat"
New-PSSession -ComputerName $hostname -Credential *
$gettime = (Get-Date).AddMinutes(1)
$run = $gettime.ToString('HH:mm')
$action = New-ScheduledTaskAction -Execute 'C:\Test'
$trigger = New-ScheduledTaskTrigger -Once -At $run
$principal = New-ScheduledTaskPrincipal -GroupID "BUILTIN\Administrators" -RunLevel Highest
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName "Install" -Description "Test"
pause
}
Install
答案 0 :(得分:0)
脚本中New-PSSession -ComputerName $hostname -Credential *
的目的是什么?
如果您试图在远程计算机上创建计划的任务,请为本地计算机创建脚本,一旦脚本在本地计算机上运行,则将其放入脚本块中并使用Invoke-Command
$Credes = Get-Credential
Invoke-Command -ComputerName $hostname -Credential $Credes -Scriptblock {
$action = New-ScheduledTaskAction -Execute 'C:\Test'
$trigger = New-ScheduledTaskTrigger -Once -At $run
$principal = New-ScheduledTaskPrincipal -GroupID "BUILTIN\Administrators" -RunLevel Highest
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal - TaskName "Install" -Description "Test"
}