Folder permissions through powershell - active-directory

im trying to set some rights on a newly created user in AD.
After I have created the folder, I try to set the various rights like this:
$Rights = [System.Security.AccessControl.FileSystemRights]::FullControl
$Inherit = #([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
$Propagation = [System.Security.AccessControl.PropagationFlags]::None
$Access =[System.Security.AccessControl.AccessControlType]::Allow
$ACL = New-Object System.Security.Principal.NTAccount "localdomain\$userprincipalname"
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($ACL, $Rights, $Inherit, $Propagation, $Access)
$ACL = Get-Acl -Path $userDir
$ACL.AddAccessRule($objACE)
Set-ACL -Path $userDir -AclObject $ACL
The error I get is related to the parameters i pass to AddAccessRule
Exception calling "AddAccessRule" with "1" argument(s): "Some or all identity references could not be translated."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
But I cannot see any error here, so I would really appreciate another set of eyes.

Ok so my solution works, and as far as I can find, is the way to set rights on a folder.
$Rights = [System.Security.AccessControl.FileSystemRights]::FullControl
$Inherit = #([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
$Propagation = [System.Security.AccessControl.PropagationFlags]::None
$Access =[System.Security.AccessControl.AccessControlType]::Allow
$ACL = New-Object System.Security.Principal.NTAccount "localdomain\$userprincipalname"
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($ACL, $Rights,$Inherit, $Propagation, $Access)
$ACL = Get-Acl -Path $userDir
$ACL.AddAccessRule($objACE)
Set-ACL -Path $userDir -AclObject $ACL

Related

Powershell SQL Server stored procedure backup

I am trying to backup one particular stored procedure from a SQL Server database by passing parameters from a Python program. Here is the code that I have tried but I keep getting an error.
param([string]$server='dbsed0898', [string]$dbname='global_hub',[string]$sp='dbo.gs_eligibility')
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) | out-null
$SMOserver = 'Microsoft.SqlServer.Management.Smo' #-argumentlist $server
$srv = New-Object("$SMOserver.Server") $server
$db = $srv.databases[$dbname]
$Objects = $db.storedprocedures[$sp]
$scripter = new-object ("$SMOserver.Scripter") $srv
$Scripter.Script($Objects) | Out-File
" C:\Users\fthoma15\Documents\backup_03212020.sql"
$db = $SMOserver.databases[$dbname]
$Objects = $db.storedprocedures[$sp]
$Scripter.Script($Objects) | Out-File
"C:\Users\fthoma15\Documents\backup_03212020.sql"
Error:
Multiple ambiguous overloads found for "Script" and the argument count: "1".
At line:12 char:5
+ $Scripter.Script($Objects) | Out-File "C:\Users\fthoma15\Document ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Can someone help me?
Here is what i did.
param([string]$server='test', [string]$dbname='test',[string[]]$sp=('test','test'))
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.SMO”) | out-
null
$SMOserver = new-object ("Microsoft.SqlServer.Management.Smo.Scripter") #-argumentlist
$server
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("$server")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("$dbname")
$Objects = $db.storedprocedures[$sp[1,3]]
$scripter = new-object ("$SMOserver") $srv
$Scripter.Script($Objects) | Out-File
"C:\Users\fthoma15\Documents\backup_03212020.sql"
As suggested by AlwaysLearning, i changed the sp variable to an array list,splitting both schema and sp name.

Issue with Powershell SMO SQL Server restore

I am running into an issue with doing a Powershell SQL restore. I have this code
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
$backupFile = 'C:\Temp\User_20191029152532.bak'
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)"
$backupDevice = New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($backupFile, "File")
$smoRestore = new-object("Microsoft.SqlServer.Management.Smo.Restore")
$smoRestore.NoRecovery = $false;
$smoRestore.ReplaceDatabase = $true;
$smoRestore.Action = "Database"
$smoRestorePercentCompleteNotification = 10;
$smoRestore.Devices.Add($backupDevice)
$smoRestoreDetails = $smoRestore.ReadBackupHeader($server)
"Database Name from Backup Header : " +$smoRestoreDetails.Rows[0]["DatabaseName"]
$smoRestore.Database =$smoRestoreDetails.Rows[0]["DatabaseName"]
$smoRestoreFile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile")
$smoRestoreLog = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile")
$smoRestoreFile.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"]
$smoRestoreFile.PhysicalFileName = $server.Information.MasterDBPath + "\" + $smoRestore.Database + "_Data.mdf"
$smoRestoreLog.LogicalFileName = $smoRestoreDetails.Rows[0]["DatabaseName"] + "_Log"
$smoRestoreLog.PhysicalFileName = $server.Information.MasterDBLogPath + "\" + $smoRestore.Database + "_Log.ldf"
$smoRestore.RelocateFiles.Add($smoRestoreFile)
$smoRestore.RelocateFiles.Add($smoRestoreLog)
$smoRestore.SqlRestore($server)
Everything works fine if I run this. However, I would like to provide a variable for which backup to restore. So I changed the top part of the code to this:
$path = 'C:\Temp'
$db_name = 'User'
$file = Get-ChildItem $path '*.bak' | Select-Object basename | Where-Object {$_.basename -like $db_name + '*' }
$backupFile = $path + '\' + $file
When I make this change, I start getting the below error.
Exception calling "ReadBackupHeader" with "1" argument(s): "An exception occurred while executing a Transact-SQL statement or batch."
At line:23 char:1
+ $smoRestoreDetails = $smoRestore.ReadBackupHeader($server)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ExecutionFailureException
Not really sure why it will not accept the $file variable that I am passing. If I look at the variable, it is returning the right value.
Stupid issue on my part. $file did not include the extension. Adding the .bak to $file fixed the issue.

Can connect to SQL Server from SSMS from other computer but Can't connect to SQL Server in a network from powershell

i am trying to connect to a sql server from powershell and getting this error
Exception calling "Open" with "0" argument(s): "The target principal
name is incorrect. Cannot generate SSPI context." At
C:\Users\Musawwir\Downloads\remotely access db.ps1:10 char:1
+ $Connection.Open()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ( [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException Exception calling "Fill" with "1" argument(s):
I can connect to SQL SERVER from other machines through SSMS but can not connect through powershell, also the script runs fine on local machine.
Here is the code i am using :
[string] $Server= "tcp:DESKTOP-J9UQ90E,1433"
[string] $Database = "Eshop"
[string] $SQLQuery= "[dbo].[usp_getCustomerAge]"
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';Connection Timeout=300;Integrated Security=TRUE;UID=ali;PWD=1234"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Command.CommandTimeout=500
$adp = New-Object System.Data.SqlClient.SqlDataAdapter $Command
$data = New-Object System.Data.DataSet
$adp.Fill($data) | Out-Null
$data2 = ''
$emp = ''
foreach($Row in $data.Tables.Rows){
[string]$C_name = $Row[0]
$C_name
$data2= $data2+ "|"+$C_name
$C_name = $C_name+"|"
$C_name | out-file "d:\\test4.txt" -Append
}
$Connection.Close()
#$data2 | out-file "d:\\test4.txt"
The user ali wasn't having access to stored procedure .. changed user from ali to sa and integrated security to false solved the problem

Using Invoke-Command to Get Application Pools on Remote Servers

I'm attempting to use Invoke-Command to get a list of application pools on multiple remote servers. So far I have something like:
$servers = Get-Content -Path "C:\Path\to\servers.txt"
$array = New-Object -TypeName 'System.Collections.ArrayList'
foreach ($server in $servers) {
Invoke-Command -ComputerName $server -ScriptBlock {
Import-Module WebAdministration
$sites = Get-ChildItem IIS:\sites
foreach ($site in $sites) {
$array.Add($site.bindings)}}}
However I get the error:
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : computername
I've tried using regular arrays instead of ArrayLists and I get the following error:
Method invocation failed because [Microsoft.IIs.PowerShell.Framework.ConfigurationElement] doesn't contain a method named 'op_Addition'.
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
+ PSComputerName : computername
Can anyone help point me in the right direction?
Your object $arrayis not known on your remote servers. So, one proposition is to send your array to the remote server, then add some values and return it :
$servers = Get-Content -Path "C:\Path\to\servers.txt"
$array = New-Object -TypeName 'System.Collections.ArrayList'
foreach ($server in $servers) {
$array = Invoke-Command -ComputerName $server -ScriptBlock {
param($array)
Import-Module WebAdministration
$sites = Get-ChildItem IIS:\sites
foreach ($site in $sites) {
[void]($array.Add($site.bindings))
}
$array
} -ArgumentList $array
}

Powershell error: “Cannot convert value of type ”Microsoft.SqlServer.Management.Smo.Server“ to type…

The answer provided for this question is unacceptable.
Error I'm getting from Powershell:
Cannot convert argument "srv", with value: "[DBADEV\SQL2008r2]", for "SqlBackup" to type "Microsoft.SqlServer.Management.Smo.Server": "Cannot convert the "[DBADEV\SQL2008r2]" value of type
"Microsoft.SqlServer.Management.Smo.Server" to type "Microsoft.SqlServer.Management.Smo.Server"."
At line:23 char:1
+ $backup.SqlBackup($srv)
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
I'm attempting to write a PowerShell script to restore a database from our Production box and into our DBADEV box. Below is the code I'm using which is then producing the error.
#Clear Screen
cls
#load assemblies
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
$ErrorActionPreference = "Inquire"
# Restore [SQLSRV2k8-0102\SQL2008] instance
$BackupFile = Get-ChildItem "\\NetworkShare\r`$\MSSQL10.SQL2008\MSSQL\Backup\AdventureWorks2008r2" | select -Last 1
$BackupFile
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server("DBADEV\SQL2008r2")
$res = New-Object Microsoft.SqlServer.Management.Smo.Restore
$backup = New-Object Microsoft.SqlServer.Management.Smo.Backup
$Backup.Devices.AddDevice($BackupFile, [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
$Backup.Database = "AdventureWorks2008r2"
$Backup.Action = [Microsoft.SqlServer.Management.Smo.BackupActionType]::Database
$backup.Initialize = $true
$backup.SqlBackup($srv)
$srv.Databases["AdventureWorks2008r2"].Drop()
$res.Devices.AddDevice($BackupFile, [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
$res.Database = "AdventureWorks2008r2"
$res.NoRecovery = $true
$res.SqlRestore($srv)
The error seems a bit cryptic to me (as do most PowerShell errors). Any thoughts on why this is occurring? I'm also getting the error when I use Mike Fal's powershell script here: http://www.mikefal.net/2014/07/22/restoreautomation-powershell-module/

Resources