Retrieving Remote File Share 'Share Permissions' Using Powershell - powershell

I have a share hosted on another computer FILESERVER1 at \\FILESERVER1\FILESHARE1.
How do I view the Share Permissions of this folder using PowerShell? Is this possible?
Please note, I am NOT referring to NTFS permissions.

Get-SmbShare and Get-SmbShareAccess seem to be what you want, as long as you have PowerShell 5. Those commands are not present on a Windows 7/PSh4 computer I'm using.
You can run the commands on the remote computer by using a session:
$s = New-PsSession -ComputerName FILESERVER1
Invoke-Command -Session $s {Get-SmbShare}
Invoke-Command -Session $s {Get-SmbShareAccess 'FILESHARE1'}

Related

Enter-PSSession equivalent of $profile script

On my local PC and locally on the servers I admin, I regularly use the $profile script to set/output basic information. For instance running Set-Location to set the current path to the folder containing the scripts, and perhaps some Write-Host entries to show a basic cheat sheet for the most commonly used scripts and their expected parameters.
Does anyone know of a way to do something similar to that when using Enter-PSSession to connect interactively with a remote server?
As far as I can see there are no $profile files available with remote sessions, so I can't just add the commands in there (and the $profile used interactively on the local server doesn't get called when you remote into that same server).
Locally I've added functions to my local profile to make connecting to specific servers quicker, for example :
function foo{
$host.ui.RawUI.WindowTitle = "Foo"
Enter-PSSession -computername foo.local.mydomain.com -authentication credssp -credential mydomain\adminuser
}
and that works fine for connecting me (eg I type foo, then enter my password, and I'm in), but I still get dumped into C:\Users\adminuser\Documents.
I've tried adding things like the Set-Location command to the function after the connection, but that gets run in the local context (where the folder doesn't exist) and THEN it connects to the server. I even tried piping the commands to Enter-PSSession, but perhaps unsuprisingly that didn't work either.
Obviously things like Invoke-Command would allow me to specify commands to run once connected, but that wouldn't (as far as I can work out) leave me with an interactive session which is the primary aim.
You can't really automate unattended execution of anything that happens after Enter-PSSession connects your host to the remote session - but you can execute all the code you want in the remote session before calling Enter-PSSession:
function DumpMeInto {
param([string]$Path)
# Create remote session (you'll be prompted for credentials at this point)
$session = New-PSSession -ComputerName foo.local.mydomain.com -Authentication credssp -Credential mydomain\adminuser
# Run Set-Location in remote runspace
Invoke-Command -Session $session -ScriptBlock { Set-Location $args[0] } -ArgumentList $Path
# ... and then enter the session
Enter-PSSession -Session $session
}
Now you can do DumpMeInto C:\temp and it should drop you into a remote session on foo.local.mydomain.com with it's working directory set to c:\temp

Powershell script guidance

I am looking for powershell code for installing software packages in remote machines which are in ADS domain.While installing I have to pass my admin credentials. How can I do this?
Guidance required
You can store your password to be used on a remote computer using the Get-Credential command like this:
`$Credential = Get-Credential
You'll see a prompt like this appear:
I would recommend storing the applications you need to install in a central place, that all of your remote devices can reach. I'll assume you've stored them in the UNC Path: \\FileServer\Application
Let's say you wanted to install 7Zip and had it present in that path:
$Credential = Get-Credential
$Computers = 'RemotePC1', 'RemotePC2'
Invoke-Command -ComputerName $Computers -Credential $Credential `
-ScriptBlock {& \\FileServer\Application\7Zip.msi} -ArgumentList '/q INSTALLDIR="C:\Program Files\7-Zip"'

Trigger external remote script

I have a powershell script located on a different server, and I want to trigger this script from the build server(TFS 2018) using a network path.
//MyServer/MyScripts/run.ps1
In the Powershell build step I need to specify script path under TFS source path. Is there a way to trigger the external script?
This is what I've tried without success....
Invoke-Command -ComputerName MyServer -ScriptBlock {Get-Process -Name //MyServer/MyScripts/run.ps1} -ArgumentList 'arg1', 'arg2'
Something like this:
Invoke-Command -Session $session -scriptblock {Pushd $rootFolder
.\$scriptname.ps1}
Or you could Invoke-Command -FilePath
Depends on where you want to run it.

Launching a batch file on a remote server using invoke-command

Recently I've been having a slight problem with executing batch files on a server. Whenever I try to use Invoke-Command to launch a bat file (which simply creates a txt file for testing purposes), it doesn't return any errors nor creates the text file. I've been googling that thing for hours but didn't manage to find any helpful information. Perhaps I don't have specific permissions?
I use CredSSP for authentication. And I need to use Invoke-Command with ScriptBlock.
Code that I've been using:
Batch:
echo 123>test.txt
Powershell:
invoke-command -ComputerName $s -ScriptBlock $block -Credential $cred -Authentication CredSSP -ArgumentList $test

PowerShell - Execute PowerShell Script on the C: drive of a remote server

PowerShell Version 5 build 10586
I am using the following code to remote connect to a server from my local PC
$cimSession = New-CimSession -ComputerName "SERVERNAME.DOMAIN.COM"
Invoke-Command -ComputerName SERVERNAME -ScriptBlock {Get-ChildItem “C:\Temp\ps”}
Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1"
Remove-CimSession -CimSession $cimSession
The first command is able to run successfully and sees the PowerShell file on the remote server.
The second command fails with an error:
Invoke-Command : Cannot find path '\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1' because it does not exist.
Is there another way to call/run a PowerShell script on the C Drive of a remote server?
I have granted my account full access to the specified file.
I have also tried to share the specified folder and given myself Read/Write access to the folder.
I have changed the file path to the share path and get the same result.
Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\ps\PS_SCRIPT_FILE.ps1"
Try this:
Invoke-Command -ComputerName SERVERNAME -ScriptBlock { Invoke-Command -FilePath "C:\Temp\ps\PS_SCRIPT_FILE.ps1" }
In your existing code the -FilePath parameter is processed on the calling machine. By including that as a parameter within the ScriptBlock it should be processed on the target machine.