简体   繁体   中英

Chng Execution Policy in RemotePSSession

I am trying to write something that will prompt for a PC name and then remotely perform a folder permission change. Everything is chaotic looking but I am struggling with changing the execution policy after the Enter-PSSession - it seems it just gets stuck and won't proceed further to my function.

This is the rough draft of what I have thus far. Any help is appreciated.

$PromptedPC = Read-Host -Prompt "`n `n Enter PC Number" 
Enter-PSSession -ComputerName $PromptedPC


Write-Host `n"Do you want to change folder permissions?" -ForegroundColor Green
$ReadAnswer = Read-Host " ( y / n ) "
PowerShell.exe -noprofile -ExecutionPolicy Bypass

switch ($ReadAnswer)
    { 
      Y {

function Grant-userFullRights {            
 [cmdletbinding()]            
 param(            
 [Parameter(Mandatory=$true)]            
 [string[]]$Files,            
 [Parameter(Mandatory=$true)]            
 [string]$UserName            
 )            
 $rule=new-object System.Security.AccessControl.FileSystemAccessRule ($UserName,"FullControl","Allow")            

 foreach($File in $Files) {            
  if(Test-Path $File) {            
   try {            
    $acl = Get-ACL -Path $File -ErrorAction stop            
    $acl.SetAccessRule($rule)            
    Set-ACL -Path $File -ACLObject $acl -ErrorAction stop            
    Write-Host "Successfully set permissions on $File"            
   } catch {            
    Write-Warning "$File : Failed to set perms. Details : $_"          
    Continue            
   }            
  } else {            
   Write-Warning "$File : No such file found"           
   Continue            
  }            
 }            
}
}
}
Grant-userFullRights -Files 'C:\ProgramData\New World Systems\' -UserName "BUILTIN\Users"

I did get the function information from: http://techibee.com/powershell/grant-fullcontrol-permission-to-usergroup-on-filefolder-using-powershell/2158

I am just trying to put something together that will work for a programdata folder our users need full control of that keeps reverting permissions to something lower than full access.

You start a new PowerShell with the ExecutionPolicy Bypass.

Sine you are already in PowerShell, the proper way to configure the Execution Polcy is Set-ExecutionPolicy .

However, since you don't invoke any script file, there is no need to change the Execution Policy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM