Trying to create a SMB share on remote server

Arwen 21 Reputation points
2021-02-25T16:35:24.6+00:00

I'm trying to create a powershell script that part of it creates a SMB share on our file server. I first tried doing with by entering a PSSession, but the commands after the PSSession never run. It's as if it doesn't enter the PSSession until the after the script runs. So I looked at the invoke-command. I first started off by using

Invoke-Command -ComputerName servername -Credential $cred {
G:
cd $project_folder
mkdir $username
New-SmbShare -Name $username -Path "G:\home\"+$username -ChangeAccess "NTGROUP\$username" -FullAccess "domain\Domain Admins"
}

However, this doesn't work as I get access denied and that -Path is null. I tried many different ways to specify what the path is. I even put the new-smbshare in to a variable outside of the invoke-command and just ran the variable. Doesn't help. I know I need to run the command as administrator, but the rest of the script won't work if I run the whole thing as administrator. So I guess I need a way to run just this one part as administrator and fix the -path part. I was looking at the start-process command, but it seems that works best if you're calling another script. I'm trying to keep everything in one script if possible. Any ideas??

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2021-02-25T17:13:03.69+00:00

    The scriptblock needs to refer back to the variables in the calling script with "$using:".

      Invoke-Command -ComputerName servername -Credential $cred {
          "Process project {0} for user {1}." -f $using:project_folder, $using:username 
          G:
          cd $using:project_folder
          mkdir $using:username
          New-SmbShare -Name $using:username -Path "G:\home\"+$using:username -ChangeAccess "NTGROUP\$using:username" -FullAccess "domain\Domain Admins"
      }
    

    https://adamtheautomator.com/invoke-command/


1 additional answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-02-25T22:54:22.713+00:00

    I do not have an AD environment where I can test. Let's see which account is causing the problem and if we can list off some group membership.

    Replace the New-SmbShare line with these statements.

    $mydomain = "TheNameOfYourDomain"        # put the name of your domain in this variable.
    New-SmbShare -Name $using:username -Path $myPath  -FullAccess "$mydomain\Domain Admins"
    remove-smbshare -Name $using:username -force
    New-SmbShare -Name $using:username -Path $myPath -ChangeAccess "$mydomain\$using:username" 
    remove-smbshare -Name $using:username -force
    New-SmbShare -Name $using:username -Path $myPath -ChangeAccess "$mydomain\$using:username" -FullAccess "$mydomain\Domain Admins"
    
    "Just for fun, lets list off some groups to see if it can resolve AD user/groups."
    "In case we have a double hop problem talking to domain controllers."
    Get-LocalGroupmember -name administrators 
    Get-LocalGroupmember -name users
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.