FN-GM Posted August 7, 2013 Posted August 7, 2013 Hi, I have a powershell script that does a few bit. I need it so a none powershell program (a .exe) will pickup a powershell Verifiable (is that the word) For example $username = Read-Host "Enter Username" new-item -itemtype directory -path "C:\$Username program.exe -t domain\$Username Creating the folder works (just an example). It used the username entered when the script is run. How do i get the $username into the program.exe command further down? Thanks
jklight Posted August 7, 2013 Posted August 7, 2013 I use gam to manage my google apps for education so just for fun i tried this: $username = Read-Host "Enter Username" new-item -itemtype directory -path "C:\$Username" c:\gam\gam.exe user $Username language en-GB Worked like a champ! created the folder and modified the users setting in Google apps.
Cache Posted August 7, 2013 Posted August 7, 2013 (edited) In my user creation script I knocked up before the end of term I used the following to set the owner with icalcs within powershell. icacls.exe $userfolderdirectory /setowner $domuser /t /c /q I assume your getting an error when you run your script, so how about chucking it into a domuser variable like I did (I can't remember why I did it, maybe just because I've used it several times in different places for my script) So your code would become: $username = Read-Host "Enter Username" $domuser = "domain\" + $username new-item -itemtype directory -path "C:\$Username program.exe -t $domuser The other thing I might suggest would be what happens if you just use Username and at the prompt type domain\username? Edited August 7, 2013 by Cache
Arthur Posted August 7, 2013 Posted August 7, 2013 (edited) what happens if you just use username and at the prompt type domain\username? Use parameters, make the username variable mandatory and then validate it with a regular expression so that it's always entered as "Domain\Username". There's obviously a lot more you can do to validate the input, but the example below should give you the general idea. function New-UserArea { param ( [Parameter(Mandatory=$true, HelpMessage="Enter a username in the format domain\username.")] [ValidatePattern("\w+\\\w+")] [string]$Username ) begin { ### Set Variables ### # Username (with domain part removed) $uwd = $Username.split("\")[1] # Path to folder $path = "$env:SystemDrive\$uwd" } process { # Check folder doesn't exist already If (-not (Test-Path $path -PathType Container)) { # Create folder New-Item -Path $path -Type Directory -Force # Set '$Username' as the owner $acl = Get-Acl $path $acl.SetOwner([system.Security.Principal.NTAccount] "$Username") Set-Acl $path $acl } } } To use the function above (assuming New-UserArea.ps1 is saved on the desktop): http://i.imgur.com/mjgfyVV.png or http://i.imgur.com/A9LooA2.png As the screenshots above show, you can either specify the username as a parameter on the command-line or type the name of the function and it will prompt you for the username. pickup a powershell verifiable (is that the word) Your thread should be titled "PowerShell variable in non-PowerShell command". Edited August 7, 2013 by Arthur
FN-GM Posted August 8, 2013 Author Posted August 8, 2013 In my user creation script I knocked up before the end of term I used the following to set the owner with icalcs within powershell. icacls.exe $userfolderdirectory /setowner $domuser /t /c /q I assume your getting an error when you run your script, so how about chucking it into a domuser variable like I did (I can't remember why I did it, maybe just because I've used it several times in different places for my script) So your code would become: $username = Read-Host "Enter Username" $domuser = "domain\" + $username new-item -itemtype directory -path "C:\$Username program.exe -t $domuser The other thing I might suggest would be what happens if you just use Username and at the prompt type domain\username? Cheers. I think you are on the right path. I need to tweak it It needs to be program.exe -t doamin\username:P What would that need to be please? Thanks
Cache Posted August 8, 2013 Posted August 8, 2013 (edited) Cheers. I think you are on the right path. I need to tweak it It needs to be program.exe -t doamin\username:P What would that need to be please? Thanks This is where my ignorance of powershell shows as I learned just what I needed to create my user script. I would guess it will either be: program.exe -t $domuser:P or if you need to escape the : (don't know whether you do or not) program.exe -t $domuser' or if neither of those worked the last thing I'd try (and might help meet your needs better if it does work) would be to try and create a string for the program and just passing it that: $username = Read-Host "Enter Username" $domuser = "domain\" + $username new-item -itemtype directory -path "C:\$Username $programparams = "-t " +$domuser + ":P" program.exe $programparams The only external program I've called is icalcs, if it doesn't work then perhaps @Arthur might be able to provide better advice? It's at this point, like you, I'd come asking for help Edited August 8, 2013 by Cache
Arthur Posted August 8, 2013 Posted August 8, 2013 It needs to be program.exe -t domain\username:P Try this... Start-Process -FilePath "program.exe" -ArgumentList "-t domain\$username:P" -NoNewWindow -PassThru -Wait Is program.exe in the same folder as the PowerShell script or is it a command that's included with Windows?
FN-GM Posted August 9, 2013 Author Posted August 9, 2013 I am basically trying to use Calcs to set permissions. The powershell way looks complicated but this looks just as bad.
Cache Posted August 9, 2013 Posted August 9, 2013 (edited) I've extracted the variables and bit of script I use for student home directories if that helps, it was fairly easy to use Powershell for the permissions bit (a lot of it was making sure inheritance was right and then just adding the user to it with Full Control) it was changing the owner I could never get to work: $userfolderdirectory = "E:\Users\Students\Intake" + $_.IntakeYear +"\" + $_.SamAccountName $domuser = "domain\" + $_.SamAccountName $sharename = $_.SamAccountName + "$" ##Create HomeDirectory and subfolders # Create Home Directory and the associated redirected folders New-Item -Path $userfolderdirectory -ItemType directory New-Item -Path $userfolderdirectory"\Documents" -ItemType directory New-Item -Path $userfolderdirectory"\Music" -ItemType directory New-Item -Path $userfolderdirectory"\Pictures" -ItemType directory New-Item -Path $userfolderdirectory"\Settings" -ItemType directory New-Item -Path $userfolderdirectory"\Downloads" -ItemType directory New-Item -Path $userfolderdirectory"\Settings\Favorites" -ItemType directory New-Item -Path $userfolderdirectory"\Settings\Links" -ItemType directory New-Item -Path $userfolderdirectory"\Videos" -ItemType directory # Create Subject Folders in Documents Directory New-Item -Path $userfolderdirectory"\Documents\Art" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\Citizenship" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\Drama" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\DT" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\English" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\History" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\ICT" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\IVB" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\Maths" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\PE" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\Science" -ItemType directory New-Item -Path $userfolderdirectory"\Documents\Spanish" -ItemType directory ## Set permissions and Change Ownership # Use Powershell to add the user with Full Control to the Folder $Acl = Get-Acl $userfolderdirectory $Ar = New-Object system.security.accesscontrol.filesystemaccessrule($domuser,"FullControl","ContainerInherit, ObjectInherit","None","Allow") $Acl.SetAccessRule($Ar) Set-Acl $userfolderdirectory $Acl # Use icacls to set the user as the Owner of the folder and Sub Directory icacls.exe $userfolderdirectory /setowner $domuser /t /c /q ## Share the Home Directory # Use Powershell to create the shared folder New-SmbShare -Name $sharename -Path $userfolderdirectory -FullAccess Everyone It won't be the recommended way of doing it and it doesn't use functions or anything fancy like that, but it's worked for me so far.... which is all I'm bothered about Edited August 9, 2013 by Cache
jklight Posted August 9, 2013 Posted August 9, 2013 my script for creating students has this to set owner $acl.SetOwner([system.Security.Principal.NTAccount] $Logon) I remember it being a lot of trouble to figure out!
jklight Posted August 9, 2013 Posted August 9, 2013 please don't use cacls.exe (unless you have Windows Server 2000!) use icacls.exe cacls has always had problems and even icacls had problems in Server 2003 and 2008 but i am sure i read (and tested) that in 2008 R2 and later those have been fixed!
Arthur Posted August 9, 2013 Posted August 9, 2013 please don't use cacls.exe +1. Use icacls, SetACL or PowerShell itself to set permissions. Using 'calcs' is asking for trouble.
Cache Posted August 9, 2013 Posted August 9, 2013 my script for creating students has this to set owner $acl.SetOwner([system.Security.Principal.NTAccount] $Logon) I remember it being a lot of trouble to figure out! I tried numerous versions of that, but every time just kept getting Access Denied in my script and could never work it out. Hence I went to icacls.
FN-GM Posted August 9, 2013 Author Posted August 9, 2013 Thanks for the help, i am off work next week but will have a bash when i am back Thanks
Arthur Posted August 9, 2013 Posted August 9, 2013 I tried numerous versions of that, but every time just kept getting Access Denied in my script and could never work it out Did you run it from an elevated PowerShell prompt? The part of the script that sets the owner in post #4 worked for me when I tried it. i.e. $path = "$env:SystemDrive\Example" $acl = Get-Acl $path $acl.SetOwner([system.Security.Principal.NTAccount] "Administrators") Set-Acl $path $acl
Cache Posted August 10, 2013 Posted August 10, 2013 Did you run it from an elevated PowerShell prompt? The part of the script that sets the owner in post #4 worked for me when I tried it. i.e. $path = "$env:SystemDrive\Example" $acl = Get-Acl $path $acl.SetOwner([system.Security.Principal.NTAccount] "Administrators") Set-Acl $path $acl Yup, I tried elevated and normal, both just gave Access Denied. It was something to do with Powershell, Windows permissions and using local drives/paths that I didn't really understand but trying to change it to the user who's home directory it was always gave Access Denied.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now