Powershell, how to add permission to shared folder - powershell

I have the following code which creates a shared folder
if (!(Test-Path c:\myFolder))
{
New-Item -Path 'c:\myFolder' -ItemType Directory
}
If (!(GET-WMIOBJECT Win32_Share -Filter "Name='myFolder'”))
{
$Shares.Create(“c:\myFolder”,”myFolder”,0)
}
How can i add Read/Write permission to 'Everyone' to the shared folder?
I prefer not to add external dll
Thanks

The Carbon module has an Install-Share function that will do what you need:
Install-Share -Name myFolder -Path C:\myFolder -Permissions "EVERYONE,FULL"
Internally, Install-Share is using the net share console application. I believe if you run net share /?, you'll get syntax on how to create a share from the command line.
Disclaimer: I am the owner/maintainer of Carbon.

Try the Set-SharePermission function from the ShareUtils module (http://en-us.sysadmins.lv/Lists/Posts/Post.aspx?ID=28):
Import-Module ShareUtils
Get-Share -Name myFolder |
Set-SharePermission -User Everyone -AccessType Allow -Permission Change |
Set-Share

Related

Powershell copy all folders and files with certain extension

I have one package on my Windows machine and another package on a remote server.
The first one is -> C:\Users\One. It contains the following files:
adapter.jsx
result.js
system.jsx
moment.js
readme.txt
package called info that contains two files -> logger.jsx and date.js.
Another one is a remote target directory -> /mnt/media/Two. It is currently empty. The user and host for it are: $userAndHost = "user#foo.bar"
I want to copy all the packages and files of extensions .jsx and .js from package One to package Two. It's required to use scp here since this is a copy between two different platforms.
What I tried:
get all the items within the package:
Get-ChildItem -Path "C:\Users\One" -Recurse
filter items by certain extension, in my case they are .jsx and .js:
Get-ChildItem -Path "C:\Users\One" -Recurse | Where-Object {$_.extension -in ".js",".jsx"}
do the secure copy (scp) - I didn't come up with the script here.
Please, help me finish the script.
Hi i think you need something like this.
I write a code for you, tested working.
#Set execution policy to bypass
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
#install Posh-SSH from powershell gallery
#https://www.powershellgallery.com/packages/Posh-SSH/2.0.2
Install-Module -Name Posh-SSH -RequiredVersion 2.0.2
#import module
Import-Module Posh-SSH
#get all the items within the package in the path:
$path = 'C:\Users\One'
$items = (Get-ChildItem -Path $path -Name -File -Include ( '*.jsx', '*.js') -Recurse)
#Need destination credential
$credential = Get-Credential
#copy selected items to destination via scp
$items | ForEach-Object {
Set-SCPFile -ComputerName 'SCP-SERVER-HOST-HERE' -Credential $credential -RemotePath '/mnt/media/Two' -LocalFile "$path\$_"
}
Hope this helps you

PowerShell Only get files within folders > move to root > delete folders only > upload to ShareFile

I have the below powershell script which has been developed to copy files from a local unc path to the application Citrix ShareFile. Whilst all is good on that aspect, one issue we are facing is that we strictly can not support folders due to Citrix ShareFile not accepting the copy as a new upload, however it is creating the file as a New Folder and not triggering the workflow correctly.
One thing I think that will make our life easier is simply not supporting folders which will work for our environment.
What i am thinking is a script that pulls all files and moves them to the root directory, deletes all folders, then uploads them to ShareFile.
The below script will copy the folder and all its contents.
I have had a look around and am struggling to get it to do as i wish.
## Add ShareFile PowerShell Snap-in
Add-PSSnapin ShareFile
## Create new authentication file
#New-SfClient -Name "C:\Sharefile\SVCACC.sfps" -Account aws
## Variables ##
$OutputAppReqFID = "fo4a3b58-bdd6-44c8-ba11-763e211c183f"
$Project = 'A001'
$LocalPath = "\\file.server.au\$project\DATA\DATA CUSTODIAN\OUTPUT\"
$sfClient = Get-SfClient -Name C:\sharefile\SVCACC.sfps
$OutputAppReqFID_URL = (Send-SfRequest $sfClient -Entity Items -id $OutputAppReqFID).Url
## Create PS Drive ##
New-PSDrive -Name "sfDrive-$($project)" -PSProvider ShareFile -Client $sfClient -Root "\" -RootUri $OutputAppReqFID_URL
## Copy all files from specified path to ShareFile, followed by moving files to another folder ##
foreach ($object in Get-ChildItem -Path $LocalPath) {
Copy-SfItem -Path $object.FullName -Destination "sfDrive-$($project):"
remove-item $object.FullName -Recurse
}
## Remove PS Drive ##
Remove-PSDrive "sfdrive-$($project)"
Answered!
I managed to apply a simple Where-Object that excludes the mode directory d----- from the upload
Get-childitem -Path $LocalPath -Recurse | Where-Object {$_.Mode -ne "d-----"} | Select-Object -ExpandProperty FullName)
Seems to have worked a treat!

What command line to delete folder in Appdata folder for local user?

I am trying to test a script to delete a folder in APPdata local folder for a user on machine but testing it locally on my test machine. i cannot use the %userprofile% so technically need to delete a test folder at c:/users/testusername/appdata/local/test
Whats the correct command to delete folders or files from a local user account? I don't want to use the exact name of user.
Also for power shell if i want to delete folders that begin with test_foldername is there a way i can wild card to delete anything with "test_"?
Thanks
Ended up using the following
$users = Get-ChildItem C:\Users
foreach ($user in $users){$folder = "$($user.fullname)\AppData\Local\Test"
If (Test-Path $folder) {Remove-Item $folder -Recurse -Force -ErrorAction silentlycontinue } }
Get-Childitem -Directory -Path $env:LOCALAPPDATA | where {$_.Name -like 'test_*'} | Remove-Item -Force -Recurse -WhatIf
Remove the -WhatIf to actual perform the delete
$env:LOCALAPPDATA will refer to the oaming app data location for the user running the script

Powershell Add-NTFSAccess using csv

I'm a bit new to Powershell and i'm trying to complete a simple script for a project I'm working on. I get it working 95% of the way but it just not returning the results. I have loaded the NTFSSECURITY Module and I'm using Add-NTFSACCESS to set Deny delete on multiple folders using the paths stored in a .csv file. See below.
Script
$itempath = import-csv "C:\dox\folderpath.csv"
foreach ($items in $itempath) {
Add-NTFSAccess -path $itempath -AccessRights Delete -Account "domain\username" -AccessType Deny -AppliesTo ThisFolderOnly
}
This is the error i get
Add-NTFSAccess : Unable to find the specified file.
Please help
Add-NTFSAccess -path $itempath this is telling powershell that the path to the item you want to change access to is an array of powershell objects that you imported from your CSV. You'll need to change that to $items.something with "something" being the heading of the column in your CSV file that contains the path.
OK Guys thanks a lot for your input. Its a simple script and what both of you said should work but I'm getting different errors. I found a way to do it without using a csv file but this was will apply the permission to all folders in the directory. Works for me. Please not you have to import the NTFSSECURITY Module to use Add-NTFSAccess
Get-ChildItem "\directory\path\" -Directory | % { $_.FullName} | Add-NTFSAccess -AccessRights Delete -Account "domain\username" -AccessType Deny -AppliesTo ThisFolderOnly

How to export shared folder with permissions and groups associated

I'm working on a windows server 2008 r2 and I'm trying to export the configuration of shared folder with all the groups associated to them,permissions and file system permissions.
is there a way to do that?
maybe with powershell?
#edit: another problem is that I need to do that after a reboot, so I have to save the configuration in a file for example and then reimport it.
If you want to backup/restore all existing shares you could export/import the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares.
Backup:
reg export HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares shares.reg
Restore:
reg import shares.reg
net stop server && net start server
File/folder ACLs can be saved and restored like this:
Backup:
Get-WmiObject -Class Win32_Share -Filter 'Type = 0' | select -Expand Path | % {
$path = $_
Get-Acl $path | select #{n='Path';e={$path}}, Sddl
} | Export-Csv 'C:\path\to\acls.csv'
Restore:
Import-Csv 'C:\path\to\acls.csv' | % {
$acl = Get-Acl $_.Path
$acl.SetSecurityDescriptorSddlForm($_.Sddl)
Set-Acl -Path $_.Path -AclObject $acl
}
Interesting question, I think the only way to do so is manually getting the acl on original folder and then re-apply them to the copied folder. The cmdlet to be used are Get-Acl -path $youfolder, Copy-Item and Set-Acl
I'm working on a module (see here) that should be able to do this for you. It's a script module, so you can actually open it up and look at/modify the code. If you use it, you could do something like this (the Export-Csv call is commented out, but you can put it in after confirming this is the output you're looking for):
Get-WmiObject Win32_Share -ComputerName ServerName |
Get-AccessControlEntry #| Export-Csv -Path CsvLocation.csv
You'll get errors for built-in system shares, e.g., C$, so you may want to add an -ErrorAction SilentlyContinue and/or an -ErrorVariable to the Get-AccessControlEntry call.
To bring the permissions back in, you'd just feed the Get-AccessControl output into Add-AccessControlEntry:
Import-Csv -Path CsvLocation.csv | Add-AccessControlEntry -WhatIf
Add-AccessControlEntry prompts for confirmation by default. Use the -Force switch to suppress the prompts.
Changing this to work for the NTFS permissions is very easy, too. Just change the Get-WmiObject call into a Get-ChildItem call, and everything else should be the same.