+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
Scripts Thread, Powershell Verifiable in none-Powershell Command in Coding and Web Development; Hi, I have a powershell script that does a few bit. I need it so a none powershell program (a ...
  1. #1

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,028
    Thank Post
    887
    Thanked 1,723 Times in 1,488 Posts
    Blog Entries
    12
    Rep Power
    453

    Powershell Verifiable in none-Powershell Command

    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

    Code:
    $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

  2. #2

    Join Date
    Jul 2012
    Location
    Boerne Texas USA
    Posts
    45
    Thank Post
    0
    Thanked 9 Times in 9 Posts
    Rep Power
    6
    I use gam to manage my google apps for education so just for fun i tried this:
    Code:
    $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.

  3. #3
    Cache's Avatar
    Join Date
    Apr 2008
    Location
    Cumbria
    Posts
    1,220
    Thank Post
    454
    Thanked 177 Times in 174 Posts
    Blog Entries
    3
    Rep Power
    65
    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.

    Code:
    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:

    Code:
    $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?
    Last edited by Cache; 7th August 2013 at 04:07 PM.

  4. #4


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,006
    Thank Post
    230
    Thanked 2,693 Times in 1,988 Posts
    Rep Power
    791
    Quote Originally Posted by Cache View Post
    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.

    Code:
    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):



    or



    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.

    Quote Originally Posted by FN-GM View Post
    pickup a powershell verifiable (is that the word)
    Your thread should be titled "PowerShell variable in non-PowerShell command".
    Last edited by Arthur; 7th August 2013 at 11:09 PM.

  5. #5

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,028
    Thank Post
    887
    Thanked 1,723 Times in 1,488 Posts
    Blog Entries
    12
    Rep Power
    453
    Quote Originally Posted by Cache View Post
    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.

    Code:
    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:

    Code:
    $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

  6. #6
    Cache's Avatar
    Join Date
    Apr 2008
    Location
    Cumbria
    Posts
    1,220
    Thank Post
    454
    Thanked 177 Times in 174 Posts
    Blog Entries
    3
    Rep Power
    65
    Quote Originally Posted by FN-GM View Post
    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:

    Code:
    program.exe -t $domuser:P
    or if you need to escape the : (don't know whether you do or not)
    Code:
    program.exe -t $domuser':P
    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:

    Code:
    $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
    Last edited by Cache; 8th August 2013 at 12:06 PM.

  7. #7


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,006
    Thank Post
    230
    Thanked 2,693 Times in 1,988 Posts
    Rep Power
    791
    Quote Originally Posted by FN-GM View Post
    It needs to be program.exe -t domain\username:P
    Try this...

    Code:
    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?

  8. #8

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,028
    Thank Post
    887
    Thanked 1,723 Times in 1,488 Posts
    Blog Entries
    12
    Rep Power
    453
    I am basically trying to use Calcs to set permissions. The powershell way looks complicated but this looks just as bad.

  9. #9
    Cache's Avatar
    Join Date
    Apr 2008
    Location
    Cumbria
    Posts
    1,220
    Thank Post
    454
    Thanked 177 Times in 174 Posts
    Blog Entries
    3
    Rep Power
    65
    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:

    Code:
    $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
    Last edited by Cache; 9th August 2013 at 10:55 AM.

  10. #10

    Join Date
    Jul 2012
    Location
    Boerne Texas USA
    Posts
    45
    Thank Post
    0
    Thanked 9 Times in 9 Posts
    Rep Power
    6
    my script for creating students has this to set owner
    Code:
    $acl.SetOwner([System.Security.Principal.NTAccount] $Logon)
    I remember it being a lot of trouble to figure out!

  11. #11

    Join Date
    Jul 2012
    Location
    Boerne Texas USA
    Posts
    45
    Thank Post
    0
    Thanked 9 Times in 9 Posts
    Rep Power
    6
    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!

  12. #12


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,006
    Thank Post
    230
    Thanked 2,693 Times in 1,988 Posts
    Rep Power
    791
    Quote Originally Posted by jklight View Post
    please don't use cacls.exe
    +1. Use icacls, SetACL or PowerShell itself to set permissions.

    Using 'calcs' is asking for trouble.

  13. #13
    Cache's Avatar
    Join Date
    Apr 2008
    Location
    Cumbria
    Posts
    1,220
    Thank Post
    454
    Thanked 177 Times in 174 Posts
    Blog Entries
    3
    Rep Power
    65
    Quote Originally Posted by jklight View Post
    my script for creating students has this to set owner
    Code:
    $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.

  14. #14

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    16,028
    Thank Post
    887
    Thanked 1,723 Times in 1,488 Posts
    Blog Entries
    12
    Rep Power
    453
    Thanks for the help, i am off work next week but will have a bash when i am back

    Thanks

  15. #15


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    9,006
    Thank Post
    230
    Thanked 2,693 Times in 1,988 Posts
    Rep Power
    791
    Quote Originally Posted by Cache View Post
    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.

    Code:
    $path = "$env:SystemDrive\Example"
    $acl = Get-Acl $path
    
    $acl.SetOwner([System.Security.Principal.NTAccount] "Administrators")
    Set-Acl $path $acl

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Create Program in SCCM via Command Prompt
    By FN-GM in forum Enterprise Software
    Replies: 1
    Last Post: 26th November 2012, 08:08 AM
  2. Exchange Powershell command help
    By bodminman in forum Windows Server 2008 R2
    Replies: 2
    Last Post: 28th September 2010, 06:42 PM
  3. GPO or WSUS ? For deploying Powershell 2.0 automatically in my domain ?
    By albertwt in forum Windows Server 2000/2003
    Replies: 2
    Last Post: 19th April 2010, 03:32 AM
  4. Powershell modify multi-string values in AD
    By maf_001 in forum Scripts
    Replies: 1
    Last Post: 28th August 2009, 01:12 PM
  5. Replies: 0
    Last Post: 24th January 2008, 02:58 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •