#Load Snapin Add-PSSnapin ShareFile #Workdir #This is where the Script and all Credentials are stored $workDir = "D:\CloudStation\Scripts\ShareFile" #Load Accounts $colLabs = Import-Csv "$workDir\ADNLabAccounts.csv" #Loop through all csv provided accounts foreach ($Lab in $colLabs){ Write-Host "##############################################" -ForegroundColor Yellow -BackgroundColor Black Write-Host "Doing" $Lab.Account -ForegroundColor Yellow -BackgroundColor Black #Building Credentials from loaded csv $sfUsername = $Lab."Login Name" $sfPassword = convertto-securestring -String $Lab.Password -AsPlainText -Force $sfCred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $sfUsername, $sfpassword #$sfCred = Get-Credential -Massage "$Lab.Password "-UserName $sfUsername #Name the AccountName short and pretty $sfShortAccountName = $Lab.Account.Substring(0,9) #Login to ShareFile $sfLogin = New-SfClient -Name "$workDir\$sfShortAccountName" -Credential $sfCred -Account $sfShortAccountName ###################### #Users ###################### #Fetch all Users $colAccountsEmployees = Send-SfRequest -Client $sfLogin -Entity Accounts/Employees -Select "Id,url,Email" #Select accounts to delete $colAccountsEmployeestoDelete = $colAccountsEmployees | Where-Object {$_.Email -notlike $sfUsername} #Loop through all accounts and start deleting foreach ($sfEmployee in $colAccountsEmployeestoDelete){ #Delete User Write-Host "Deleting User" $sfEmployee.Email #Find the right URI using email $sfEmployeeMail = $sfEmployee.Email.ToString() $sfEmployeeUrlHost = $SFemployee.url.Host.ToString() $sfEmployeeURI = (Send-SfRequest -Client $sfLogin -Method GET -Uri "https://$sfEmployeeUrlHost/sf/v3/Users?emailaddress=$sfEmployeeMail").url.AbsoluteUri #Prepare Command for complete delete $sfEmployeeCommand = $sfEmployeeURI + "?completely=true" #Send Command to ShareFile Send-SfRequest -Client $sfLogin -Method DELETE -Uri $sfEmployeeCommand } #Tidy Up Remove-Variable colAccountsEmployees Remove-Variable colAccountsEmployeestoDelete ###################### #StorageZones ###################### #Get Zones $colZones = Send-SfRequest -Client $sfLogin -Entity Zones #Filter Zones which are customer managed $colZonestoDelete = $colZones | where {$_.ZoneType.Enum -eq "Private"} foreach ($sfZone in $colZonestoDelete){ Write-Host "Deleting Zone" $sfZone.Name $sfZoneURI = $sfZone.url.AbsoluteUri #Prepare Command for complete delete $sfZoneCommand = $sfZoneURI + "?force=true" #Send Command to ShareFile Send-SfRequest -Client $sfLogin -Method DELETE -Uri $sfZoneCommand } #Tidy Up Remove-Variable colZones Remove-Variable colZonestoDelete }