Commands that do not run in a single script

Home Forums Scripting PowerShell Commands that do not run in a single script

This topic contains 3 replies, has 3 voices, and was last updated by  srinivasaai 6 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts

  • peterhala
    Member
    #167568

    Hi,

    To me this is an obscure problem. I have found some commands need to be separated/delayed or they do not execute. I have seen this when running commands on a remote system. For example the following script (run from a workstation against a local server) _almost_ creates a folder, shares it and then tweaks the share permissions

    New-Item \fs99e$usersTESTy9a1.McTestFace$ -itemType Directory

    Enter-PSSession -ComputerName fs99

    $cim1 = get-CimSession -ComputerName fs99

    new-smbshare -name “TESTy9a1.McTestFace$” -path e:usersTESTy9a1.McTestFace$ -CimSession $cim1

    grant-smbshareaccess TESTy9a1.McTestFace$ -accountname Everyone -accessright Full -CimSession $cim1 -Confirm:$false

    The script fails on the third line, saying smbshare is not a recognized command. If I split these commands into two files as follows, taking care NOT to close the remote session between running the scripts –

    Script1:

    New-Item \fs99e$usersTESTy9a1.McTestFace$ -itemType Directory

    Enter-PSSession -ComputerName fs99

    Script2:

    $cim1 = get-CimSession -ComputerName fs99

    new-smbshare -name “TESTy9a1.McTestFace$” -path e:usersTESTy9a1.McTestFace$ -CimSession $cim1

    grant-smbshareaccess TESTy9a1.McTestFace$ -accountname Everyone -accessright Full -CimSession $cim1 -Confirm:$false

    It works fine; the split means my smb commands ARE being run on the remote system. However I have not changed the commands or the order in which they are run. So I’m missing something fundamental about the way these commands are queued or separated. Can you tell me what?

    Thanks!

    Pete


    wullieb1
    Moderator
    #245759

    This might sound daft but why not, if you’re already creating the PSSession, run all the commands after creating the session.

    #Connect to the remote system
    Enter-PSSession -ComputerName fs99

    #Create the folder
    New-Item e:usersTESTy9a1.McTestFace$ -itemType Directory

    #Create the new share
    new-smbshare -name “TESTy9a1.McTestFace$” -path e:usersTESTy9a1.McTestFace$
    grant-smbshareaccess TESTy9a1.McTestFace$ -accountname Everyone -accessright Full-Confirm:$false[/CODE]

    I haven’t tested this as i don’t have access to a remote system to do so at the moment but that should help you in the interim.
    [CODE]#Connect to the remote system
    Enter-PSSession -ComputerName fs99

    #Create the folder
    New-Item e:usersTESTy9a1.McTestFace$ -itemType Directory

    #Create the new share
    new-smbshare -name “TESTy9a1.McTestFace$” -path e:usersTESTy9a1.McTestFace$
    grant-smbshareaccess TESTy9a1.McTestFace$ -accountname Everyone -accessright Full-Confirm:$false[/CODE]

    I haven’t tested this as i don’t have access to a remote system to do so at the moment but that should help you in the interim.


    srinivasaai
    Member
    #392036

    Hi Wullieb1,

    Thanks for the reply. Running the commands in one go is exactly what I want to do but it fails on me. I should explain I have a script that I run from an AD member workstation. I run it from there partly because that makes me debug my code (avoid having stuff that only works in one place) and partly because I’m executing commands from several PS modules – PS, PS AD & PS Exchange. I only included the middle bit of code to avoid muddying the water but the program flow is:

    – Create a Mailbox/AD account
    – Create the folders the AD account needs
    – Lockdown/customise the AD account

    It works as three scripts but I’d like to make it one just for neatness.

    Kind regards,

    Pete


    wullieb1
    Moderator
    #245761

    Can you post the full santised script?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.