Hi ExpertsI wrote a function to see the SQL Install Features to the Grid. It's coming properly in a single column but I wanted details in two columns (for e.g first column will show the properties for e.g, Action, Features, instancename and the corresponding other column will show it's values selected / retrieved from the .INI file.Please help me modifying the script.function AcceptedFeatures(){$ACTION = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'ACTION='} $pos = $ACTION.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $ACTION.Substring($pos+1)$ACTION = $rightPart -replace '"', "" $ACTION$FEATURES = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'FEATURES='} $pos = $FEATURES.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $FEATURES.Substring($pos+1)$FEATURES = $rightPart -replace "`'", "`""$FEATURES = "'" + $FEATURES +"'"$FEATURES$INSTALLSHAREDDIR = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'INSTALLSHAREDDIR='} $pos = $INSTALLSHAREDDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTALLSHAREDDIR.Substring($pos+1)$INSTALLSHAREDDIR = $rightPart -replace '"', "" $INSTALLSHAREDDIR$INSTALLSHAREDWOWDIR = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'INSTALLSHAREDWOWDIR='} $pos = $INSTALLSHAREDWOWDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTALLSHAREDWOWDIR.Substring($pos+1)$INSTALLSHAREDWOWDIR = $rightPart -replace '"', "" $INSTALLSHAREDWOWDIR$INSTANCENAME = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'INSTANCENAME='} $pos = $INSTANCENAME.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTANCENAME.Substring($pos+1)$INSTANCENAME = $rightPart -replace '"', "" $INSTANCENAME$INSTANCEID = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'INSTANCEID='} $pos = $INSTANCEID.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTANCEID.Substring($pos+1)$INSTANCEID = $rightPart -replace '"', "" $INSTANCEID$INSTANCEDIR = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'INSTANCEDIR='} $pos = $INSTANCEDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTANCEDIR.Substring($pos+1)$INSTANCEDIR = $rightPart -replace '"', "" $INSTANCEDIR$AGTSVCACCOUNT = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'AGTSVCACCOUNT='} $pos = $AGTSVCACCOUNT.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $AGTSVCACCOUNT.Substring($pos+1)$AGTSVCACCOUNT = $rightPart -replace '"', "" $AGTSVCACCOUNT$AGTSVCSTARTUPTYPE = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'AGTSVCSTARTUPTYPE='} $pos = $AGTSVCSTARTUPTYPE.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $AGTSVCSTARTUPTYPE.Substring($pos+1)$AGTSVCSTARTUPTYPE = $rightPart -replace '"', "" $AGTSVCSTARTUPTYPE$SQLSVCSTARTUPTYPE = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'SQLSVCSTARTUPTYPE='} $pos = $SQLSVCSTARTUPTYPE.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLSVCSTARTUPTYPE.Substring($pos+1)$SQLSVCSTARTUPTYPE = $rightPart -replace '"', "" $SQLSVCSTARTUPTYPE$SQLCOLLATION = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'SQLCOLLATION='} $pos = $SQLCOLLATION.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLCOLLATION.Substring($pos+1)$SQLCOLLATION = $rightPart -replace '"', "" $SQLCOLLATION$SQLSVCACCOUNT = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'SQLSVCACCOUNT='} $pos = $SQLSVCACCOUNT.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLSVCACCOUNT.Substring($pos+1)$SQLSVCACCOUNT = $rightPart -replace '"', "" $SQLSVCACCOUNT$SQLSYSADMINACCOUNTS = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'SQLSYSADMINACCOUNTS='} $pos = $SQLSYSADMINACCOUNTS.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLSYSADMINACCOUNTS.Substring($pos+1)$SQLSYSADMINACCOUNTS = $rightPart -replace '"', "" $SQLSYSADMINACCOUNTS = """$SQLSYSADMINACCOUNTS""" -replace '\s', ', '$SQLSYSADMINACCOUNTS$SQLUSERDBDIR = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'SQLUSERDBDIR='} $pos = $SQLUSERDBDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLUSERDBDIR.Substring($pos+1)$SQLUSERDBDIR = $rightPart -replace '"', "" $SQLUSERDBDIR$SQLUSERDBLOGDIR = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'SQLUSERDBLOGDIR='} $pos = $SQLUSERDBLOGDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLUSERDBLOGDIR.Substring($pos+1)$SQLUSERDBLOGDIR = $rightPart -replace '"', "" $SQLUSERDBLOGDIR$SQLTEMPDBDIR = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'SQLTEMPDBDIR='} $pos = $SQLTEMPDBDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLTEMPDBDIR.Substring($pos+1)$SQLTEMPDBDIR = $rightPart -replace '"', "" $SQLTEMPDBDIR$TCPENABLED = Get-Content "C:\test\SQLServer2012ConfigurationFile_Install.ini" | Where-Object {$_ -match 'TCPENABLED='} $pos = $TCPENABLED.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $TCPENABLED.Substring($pos+1)$TCPENABLED = $rightPart -replace '"', "" IF ($TCPENABLED -eq 0){#Print $TCPENABLEDWrite-Host "Disabled"}ELSEIF($TCPENABLED -eq 1){ #Print $TCPENABLED Write-Host "Enabled" }$TCPENABLED<#$ACTION$FEATURES$INSTALLSHAREDDIR$INSTALLSHAREDWOWDIR$INSTANCENAME$INSTANCENAME$INSTANCEDIR$AGTSVCACCOUNT$AGTSVCSTARTUPTYPE$SQLSVCSTARTUPTYPE$SQLCOLLATION$SQLSVCACCOUNT$SQLSYSADMINACCOUNTS$SQLUSERDBDIR$SQLUSERDBLOGDIR$SQLTEMPDBDIR$TCPENABLED#>#$values = $ACTION #$values = + $FEATURES #$values = + $INSTALLSHAREDDIR#$values = + $INSTALLSHAREDWOWDIR#$values = + $INSTANCENAME#$values = + [string] $INSTANCENAME#$values = + $INSTANCEDIR#$values = + $AGTSVCACCOUNT#$values = + $AGTSVCSTARTUPTYPE#$values = + $SQLSVCSTARTUPTYPE#$values = + $SQLCOLLATION#$values = + $SQLSVCACCOUNT#$values = + $SQLSYSADMINACCOUNTS#$values = + $SQLUSERDBDIR#$values = + $SQLUSERDBDIR#$values = + $SQLUSERDBDIR#$values = + $SQLUSERDBLOGDIR#$values = + $SQLTEMPDBDIR#$values = + $TCPENABLED}AcceptedFeatures | out-gridview -wait
↧
Out-grid view data display
↧
Generate the results in Yellow
Is there anyway, I can format the string to generate the results in Yellow/any other color? Please suggest.Function GET-Temppassword() {Param([int]$length=10,[string[]]$sourcedata)#$alphabet="YYETHOGD5546*&6%$3#@%L";$ascii=$NULL;For ($a=33;$a –le 126;$a++) {$ascii+=,[char][byte]$a }For ($loop=1; $loop –le $length; $loop++) { $TempPassword+=($sourcedata | GET-RANDOM) }return $TempPassword}#GET-Temppassword –length 19 –sourcedata $alphabet #SEEAUBPYWBTCEFDICFZwrite-host "Your Random Generated Password Is below: `n `r" -ForegroundColor GreenGET-Temppassword –length 19 –sourcedata $ascii write-host "`n `r"
↧
↧
IF the test-path result is not correct, I don't want to see error mg, instead try max retires and then throw message
Hello,IF the test-path result is not correct, I don't want to see error mg, instead try max retires and then throw message.clear-hostfunction checkfilelocation(){ $retrycount = 0 $retries = 5 $val = 0#$args.ErrorAction = "Stop"while ($val -ne 5) { try { $PATH = read-host "Enter the complete path to read from the file: [i.e. ""C:\test\filepath.ini""; Dont include "" ""]" IF($PATH -ne '') { write-host $PATH -ForegroundColor red IF (-NOT (Test-Path -path $PATH)) { #Throw "$($PATH) is not a valid folder" $val++; write-host $bol -ForegroundColor red } } else{ write-host "Retry the path of the file once more!!" -ForegroundColor red #write-host $val; $val++; } print $val} catch [System.Exception] { write-host "Error." write-host $error[0].Exception.ToString() throw }} write-host "You did not enter the path of the file. So BREAKING.." -ForegroundColor CYAN}
↧
Unable to pass the values between functions (in Grid, no data is being displayed)
Hi ExpertsPlease help in fixing the issues. The variable values from one function to another is not being passed and thereby in show grid, no data is being displayed.Please suggest a best way to send the data:-function AcceptedFeatures{ [CmdletBinding()] Param ( [Parameter(Position=0,Mandatory=$false)][string]$action ,[Parameter(Position=1,Mandatory=$false)][string]$INSTANCENAME #for default only provide 'MSSQLSERVER', for Other named Instance just input the instance name you like. #,[Parameter(Position=2,Mandatory=$true)][string]$default #$default = 0 (for default, 1 = Named) ) Begin { $VerbosePreference = 'Continue' $DebugPreference = 'Continue' }Process{ #Int $retrycount = 0 #Int $retries = 5 $val = 0$val = [int] $val$val#$args.ErrorAction = "Stop" try { while ($val -ne 3) { $PATH = read-host "Enter the complete path to read from the file: [i.e. ""C:\test\SQLServer2012ConfigurationFile_Install.ini""; Dont include Double Quote or single Quote]" #$PATH IF($PATH -ne "") { #write-host $PATH -ForegroundColor YELLOW IF (-NOT (Test-Path -path $PATH)) { #Throw "$($PATH) is not a valid folder" write-host "Please Re-Enter the File Name with Proper Path `n `r" -ForegroundColor red $val++; write-host "testing 1:" $val } else{ write-host "Correct Filename Entered :" $PATH -ForegroundColor CYAN #$val = 5 $val ++; break } } ELSE{ write-host "testing 2:" $val write-host "Re-Enter the File Name with Proper Path `n `r" -ForegroundColor red $val++; write-host $val; } if ($val -eq 3) {return} }} catch [System.Exception] { write-host "Error." write-host $error[0].Exception.ToString() throw }#$PATH = "C:\test\SQLServer2012ConfigurationFile_Install.ini"$ACTION = Get-Content $PATH | Where-Object {$_ -match 'ACTION='} $pos = $ACTION.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $ACTION.Substring($pos+1)$ACTION = $rightPart -replace '"', "" #$Global:ACTION#$script:ACTION#$ACTION$FEATURES = Get-Content $path | Where-Object {$_ -match 'FEATURES='} $pos = $FEATURES.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $FEATURES.Substring($pos+1)$FEATURES = $rightPart -replace "`'", "`""$FEATURES = "'" + $FEATURES +"'"$Global:FEATURES#$FEATURES$INSTALLSHAREDDIR = Get-Content $path | Where-Object {$_ -match 'INSTALLSHAREDDIR='} $pos = $INSTALLSHAREDDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTALLSHAREDDIR.Substring($pos+1)$INSTALLSHAREDDIR = $rightPart -replace '"', "" $INSTALLSHAREDDIR$INSTALLSHAREDWOWDIR = Get-Content $path | Where-Object {$_ -match 'INSTALLSHAREDWOWDIR='} $pos = $INSTALLSHAREDWOWDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTALLSHAREDWOWDIR.Substring($pos+1)$INSTALLSHAREDWOWDIR = $rightPart -replace '"', "" $INSTALLSHAREDWOWDIR#$Global:INSTANCENAME#$Global:INSTANCEID$INSTANCENAME = Get-Content $path | Where-Object {$_ -match 'INSTANCENAME='} $pos = $INSTANCENAME.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTANCENAME.Substring($pos+1)$INSTANCENAME = $rightPart -replace '"', "" $INSTANCENAME$INSTANCEID = Get-Content $path | Where-Object {$_ -match 'INSTANCEID='} $pos = $INSTANCEID.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTANCEID.Substring($pos+1)$INSTANCEID = $rightPart -replace '"', "" $INSTANCEID$INSTANCEDIR = Get-Content $path | Where-Object {$_ -match 'INSTANCEDIR='} $pos = $INSTANCEDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $INSTANCEDIR.Substring($pos+1)$INSTANCEDIR = $rightPart -replace '"', "" $INSTANCEDIR$AGTSVCSTARTUPTYPE = Get-Content $path | Where-Object {$_ -match 'AGTSVCSTARTUPTYPE='} $pos = $AGTSVCSTARTUPTYPE.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $AGTSVCSTARTUPTYPE.Substring($pos+1)$AGTSVCSTARTUPTYPE = $rightPart -replace '"', "" $AGTSVCSTARTUPTYPE$SQLSVCSTARTUPTYPE = Get-Content $path | Where-Object {$_ -match 'SQLSVCSTARTUPTYPE='} $pos = $SQLSVCSTARTUPTYPE.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLSVCSTARTUPTYPE.Substring($pos+1)$SQLSVCSTARTUPTYPE = $rightPart -replace '"', "" $SQLSVCSTARTUPTYPE$SQLCOLLATION = Get-Content $path | Where-Object {$_ -match 'SQLCOLLATION='} $pos = $SQLCOLLATION.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLCOLLATION.Substring($pos+1)$SQLCOLLATION = $rightPart -replace '"', "" $SQLCOLLATION$SQLSVCACCOUNT = Get-Content $path | Where-Object {$_ -match 'SQLSYSADMINACCOUNTS='} $pos = $SQLSVCACCOUNT.IndexOf("=")$rightPart = $SQLSVCACCOUNT.Substring($pos+1)$SQLSVCACCOUNT = $rightPart -replace '"', "" $SQLSVCACCOUNT = """$SQLSVCACCOUNT""" -replace '\s', ', '$SQLSVCACCOUNT$SQLSYSADMINACCOUNTS = Get-Content $path | Where-Object {$_ -match 'SQLSYSADMINACCOUNTS='} $pos = $SQLSYSADMINACCOUNTS.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $SQLSYSADMINACCOUNTS.Substring($pos+1)$SQLSYSADMINACCOUNTS = $rightPart -replace '"', "" $SQLSYSADMINACCOUNTS = """$SQLSYSADMINACCOUNTS""" -replace '\s', ', '$SQLSYSADMINACCOUNTS$SQLUSERDBDIR = Get-Content $path | Where-Object {$_ -match 'SQLUSERDBDIR='} #$SQLTEMPDBDIR = $SQLTEMPDBDIR -replace '"', ""$pos = $SQLUSERDBDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$SQLUSERDBDIR1 = $SQLUSERDBDIR.Substring($pos+1)$SQLUSERDBDIR1 = $SQLUSERDBDIR1 -replace '"', ""$SQLUSERDBDIR = $SQLUSERDBDIR1 +'\'+$INSTANCENAME+'\'$SQLUSERDBDIR#SQLUSERDBLOGDIR PATH$SQLUSERDBLOGDIR = Get-Content $path | Where-Object {$_ -match 'SQLUSERDBLOGDIR='} #$SQLTEMPDBDIR = $SQLTEMPDBDIR -replace '"', ""$pos = $SQLUSERDBLOGDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$SQLUSERDBLOGDIR1 = $SQLUSERDBLOGDIR.Substring($pos+1)$SQLUSERDBLOGDIR1 = $SQLUSERDBLOGDIR1 -replace '"', ""$SQLUSERDBLOGDIR = $SQLUSERDBLOGDIR1 +'\'+$INSTANCENAME+'\'$SQLUSERDBLOGDIR#Tempdb path$SQLTEMPDBDIR =""$SQLTEMPDBDIR = Get-Content $path | Where-Object {$_ -match 'SQLTEMPDBDIR='} #$SQLTEMPDBDIR = $SQLTEMPDBDIR -replace '"', ""$pos = $SQLTEMPDBDIR.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$SQLTEMPDBDIR1 = $SQLTEMPDBDIR.Substring($pos+1)$SQLTEMPDBDIR1 = $SQLTEMPDBDIR1 -replace '"', ""$SQLTEMPDBDIR = $SQLTEMPDBDIR1 +'\'+$INSTANCENAME+'\'$SQLTEMPDBDIR$TCPENABLED = Get-Content $path | Where-Object {$_ -match 'TCPENABLED='} $pos = $TCPENABLED.IndexOf("=")#$leftPart = $SQLSVCACCOUNT.Substring(0, $pos)$rightPart = $TCPENABLED.Substring($pos+1)$TCPENABLED = $rightPart -replace '"', "" $TCPENABLED<#IF ($TCPENABLED -eq 0){#Print $TCPENABLEDWrite-Host "Disabled"}ELSEIF($TCPENABLED -eq 1){ #Print $TCPENABLED Write-Host "Enabled" }$TCPENABLED#>write-host ".............17 selected features during SQL Install............."$hash = @{ ACTION = $ACTION FEATURES = $FEATURES INSTALLSHAREDDIR = $INSTALLSHAREDDIR INSTALLSHAREDWOWDIR = $INSTALLSHAREDWOWDIR INSTANCENAME = $INSTANCENAME INSTANCEID = $INSTANCEID INSTANCEDIR = $INSTANCEDIR AGTSVCACCOUNT = $AGTSVCACCOUNT AGTSVCSTARTUPTYPE = $AGTSVCSTARTUPTYPE SQLSVCSTARTUPTYPE = $SQLSVCSTARTUPTYPE SQLCOLLATION = $SQLCOLLATION SQLSVCACCOUNT = $SQLSVCACCOUNT SQLSYSADMINACCOUNTS = $SQLSYSADMINACCOUNTS SQLUSERDBDIR = $SQLUSERDBDIR SQLUSERDBLOGDIR = $SQLUSERDBLOGDIR SQLTEMPDBDIR = $SQLTEMPDBDIR TCPENABLED = $TCPENABLED } $Object = New-Object PSObject -Property $hash $Objectwrite-host ".......................END OF SELECTION......................."}} # END OF fn: AcceptedFeaturesAcceptedFeaturesfunction ShowinOutGrid(){AcceptedFeatures#$List1 = "";$List1 = @()$List1 += [pscustomobject]@{Property = "Action"Value = $ACTION}$List1 += [pscustomobject]@{Property = "INSTALLSHAREDDIR"Value = $INSTALLSHAREDDIR}$List1 += [pscustomobject]@{Property = "INSTALLSHAREDWOWDIR"Value = $INSTALLSHAREDWOWDIR}$List1 += [pscustomobject]@{Property = "INSTALLSHAREDWOWDIR"Value = $INSTALLSHAREDWOWDIR}$List1 += [pscustomobject]@{Property = "INSTANCENAME"Value = $INSTANCENAME}$List1 += [pscustomobject]@{Property = "INSTANCEID"Value = $INSTANCEID}$List1 += [pscustomobject]@{Property = "INSTANCEDIR"Value = $INSTANCEDIR}$List1 += [pscustomobject]@{Property = "AGTSVCACCOUNT"Value = $AGTSVCACCOUNT}$List1 += [pscustomobject]@{Property = "AGTSVCSTARTUPTYPE"Value = $AGTSVCSTARTUPTYPE}$List1 += [pscustomobject]@{Property = "SQLSVCSTARTUPTYPE"Value = $SQLSVCSTARTUPTYPE}$List1 += [pscustomobject]@{Property = "SQLCOLLATION"Value = $SQLCOLLATION}$List1 += [pscustomobject]@{Property = "SQLSVCACCOUNT"Value = $SQLSVCACCOUNT}$List1 += [pscustomobject]@{Property = "SQLSYSADMINACCOUNTS"Value = $SQLSYSADMINACCOUNTS}$List1 += [pscustomobject]@{Property = "SQLUSERDBDIR"Value = $SQLUSERDBDIR}$List1 += [pscustomobject]@{Property = "SQLUSERDBLOGDIR"Value = $SQLUSERDBLOGDIR}$List1 += [pscustomobject]@{Property = "SQLTEMPDBDIR"Value = $SQLTEMPDBDIR}$List1 += [pscustomobject]@{Property = "TCPENABLED"Value = $TCPENABLED}$List1 | Out-GridView -wait}ShowinOutGrid
↧
Noob Powershell String Parsing Question
I am very new to Powershell and am trying to solve what appears to be a very simple problem.I have a variable called $TestLocationsThe value of $TestLocations is:[code="plain"]xxx----------2234465[/code]I need to extract the values of Lines 3,4,5 & 6 and assign their values to an array. E.g. if I had an array called #x it would look like this:#x = @((2),(23),(44),(65))
↧
↧
SQL connection using a windows functional ID
I am trying to user PowerShell to connect to a SQL server as follows:Server A - PowerShell script runs here. - Uses Microsoft.SqlServer.Smo namespace to make the connection to Server B - Using ConnectAsUsername with a active directory FID.Server B - SQL server - Active directory FID permissioned correctly to database.My problem is that on Server A, the FID needs to have the "Allow Log on locally" User Right Assignment for the connection attempt to Server B to work. I find this strange since I am not actually connecting to Server A with the FID. The error message received is the usual ""Logon failure: the user has not been granted the requested logon type at this computer.". My testing has shown that the FID does not need any URA assignments on Server B.Is there a way to avoid granting the "Allow Log on locally" User Right Assignment? Perhaps a different approach?This is the code running on Server A:[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")$sqlServer = "xxxxxxxx,1433"$serverConnection = new-object Microsoft.SqlServer.Management.Common.ServerConnection($sqlServer)$SQLQuesryResults = ""$SQLData = ""$serverConnection.ConnectAsUser = $true#$serverConnection.ConnectAsUsername = "user123@adomain.net"$serverConnection.ConnectAsUserPassword = "MyPassword"$serverConnection.Connect()
↧
Workflow Problem - Newbie
Hello - I have a Central Management Server that keeps enterprise instances. I want to select the instances from those msdb tables into an array and loop through and execute sql statements. Standard stuff. There are lots of ways to do this but I am wondering what I am doing wrong here with the [string[]]$WorkflowInstance. The array $ServerNameList is working fine but once it goes into the WorkFlow as a parameter - I get server not found errors. What am I doing wrong here?Import-Module sqlps -DisableNameChecking;WorkFlow Exec-SQLWorkflow { param( [Parameter(Mandatory=$true)] [string[]]$WorkflowInstance, [Parameter(Mandatory=$false)] [string]$Database, [Parameter(Mandatory=$true)] [string]$FilePath ) foreach -parallel ($s in $WorkflowInstance) { invoke-sqlcmd -ServerInstance $s -Database $Database -InputFile $FilePath -querytimeout 60000 -MaxCharLength 65535 -ConnectionTimeout 1 ; }} $ServerNameList = @(Invoke-Sqlcmd -Database msdb -Query 'SELECT s.name ServerNameFROM msdb.dbo.sysmanagement_shared_registered_servers_internal sJOIN msdb.dbo.sysmanagement_shared_server_groups_internal g ON s.server_group_id = g.server_group_idWHERE g.name = ''Development''ORDER BY s.name' -ServerInstance CMSSERVER2)Exec-SQLWorkflow -WorkflowInstance $ServerNameList -Database master -FilePath 'C:\SQLFlatFiles\Servers_ServerProperties.sql' -Verbose | Export-Csv -LiteralPath 'C:\Temp\processes.csv' -Delimiter '|' -NoTypeInformation
↧
How to I get/manage the Win2012R2 Cluster Resource of SQL Server Possible Owners using Powershell ?
How to I get/manage the Win2012R2 Cluster Resources [b]Possible[/b]Owners using Powershell ?[code="other"]$InstanceName = 'D006'Get-ClusterResource -Cluster cl11 -Name "SQL Server ($InstanceName)" | where ResourceType -eq 'SQL Server' | Get-ClusterOwnerNode ;[/code][code="plain"]ClusterObject OwnerNodes ------------- ---------- SQL Server (D006) {DV25, DV26, DV27} [/code]This shows the potential owner nodes of the [b]cluster [u]Role[/u][/b] !I need to remove a node from this list. If I user that using [url=https://technet.microsoft.com/en-us/library/ee461001.aspx][b]Remove-ClusterNode[/b][/url] It removes the node from the cluster. [quote]Remove a node from a failover cluster. After the node is removed, it no longer functions as part of the cluster unless you add it back to the cluster.[/quote]That is [b]not the goal[/b], because with Always On, the node has to be joined to the cluster.[img]http://www.sqlservercentral.com/Forums/Attachment17172.aspx[/img]Question is: [size="4"]How do I remove a node from the list of [b]possible[/b] owners of a sql server clustered instance ?[/size]
↧
Application Intent with Invoke-SQLcmd
Are you able to specify read-only routing in invoke-SQLcmd? $dataTable = Invoke-Sqlcmd -Query $Query -Database $SourceDatabaseName -ServerInstance $SourceServerName ;maybe add something like -READONLY = TRUE? or -APPlicationintent = 'readonly'I can't seem to find anything on it.
↧
↧
How to target files in a certain sub dir with get-childitem
Hi All, So I have a list of directories:D:\Data\Load\customer1\ArchiveD:\Data\Load\customer2\ArchiveD:\Data\Load\customer3\ArchiveD:\Data\Load\customer4\ArchiveWe receive files with spaces and i want to replace those spaces with dashes, but i want to only target items in the archive directories.Im using this but cant find the command to make it only look files in the archive folderGet-ChildItem D:\Data\Load -Filter "*.zip" -Recurse | Rename-Item -NewName {$_.name -replace ' ','-' } -whatifthis works but will also touch files in the root of the customers folder, which i would prefer not to happen.Thanks in advance!
↧
Invoke-SqlCmd output sql executed
Hi, is it possible to output the sql executed by Invoke-sqlcmd when an inputFile with variables is used ?Thanks in advance,Rui
↧
Cannot use Powershell to UNZIP files inside SQL Job
Server is running Windows 2008R2 Standard. SQL Server is 2008R2 SP2Code:$path = "I:\SQLBackupsFromPROD"$destination = "I:\SQLBackupsFromPROD\CaaS_Backups" $shell_app= New-Object -com shell.application $files = Get-ChildItem -Path $path -filter *.zip -recurse foreach($file in $files) { $zip_file = $shell_app.namespace($file.FullName) $copyHere = $shell_app.namespace($destination) $copyHere.Copyhere($zip_file.items()) }It is set to run as the SQL Server Agent Account. I have logged into the server using that account and I can run the script successfully.When I put it into a SQL Job Step and run it, it shows SUCCESS but no files are unzipped."MessageExecuted as user: <Username>. The step did not generate any output. Process Exit Code 0. The step succeeded.":crazy:
↧
implementing backup system using PS (a newbie question)
We have a backup system based on TSQL commands:1. A job in the central SQL Server initiate the process once a day.2. The backup details (which database, in which server, to which directory) are saved in a management table in the SQL Server.3. The TSQL Backup commands are executed in remote servers through Linked Servers.4. Another process deletes files older than 2 weeks from all the directories which appear in the management table (using xp_cmdshell and Del commands).How would you implement such a system using PS?I'm not just talking about replacing the TSQL commands with PS commands, but also about the general approach: Where the management data will be stored? How the execution will be initiated?
↧
↧
Powershell for network path
I am totally new to powershell(just 2 days) so please bear my ignorance.Here is the code I am trying to execute as a sql agent job that basically deletes files over 2 days old.[code]function LoopFolders([string]$path,[string]$logpath) { $fs = new-object -com scripting.filesystemobject $folder = $fs.getfolder($path) $today = get-date -uformat "%Y_%m_%d" $log = $logpath + "Deleted_" + $today + ".log" foreach ($i in $folder.files) { if($folder.path -contains "Master" -or $folder.path -contains "Model" -or $folder.path -contains "Certificates") {break} else { $dt = ((Get-Date) - $i.DateLastModified).Days if ($dt -gt 2 -and $i.PsISContainer -ne $True ) { $deleted = "Deleting - " + $i.path + " - Last Write Time - " + $i.DateLastModified add-content $log -value $deleted remove-item $i.path } } } foreach ($i in $folder.subfolders) { LoopFolders($i.path) }}LoopFolders "C:\DatabaseBackUps" "C:\DatabaseBackUps\BackUpDeleteLogs"LoopFolders "\\10.1.1.10\DatabaseBackups" "\\10.1.1.10\Database\BackUpDeleteLogs"[/code]The problem here is that the code works fine for local path but errors out for network path with the following message.[code="plain"]The corresponding line is 'remove-item $i.path'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Invalid Path: '\\10.1.1.10\database\LogBackUp\PRODUCTION_ALLIED\PRODUCTION_ALLIED_backup_2012_06_01_093000_6722391.trn'. '. Process Exit Code -1. The step failed.[/code]The same question has been asked herehttp://social.technet.microsoft.com/Forums/da-DK/winserverpowershell/thread/a1a1ba21-0bd5-4b53-8838-5930f9599024
↧
Run DTEXEC from Powershell - convert from batch file
I have the following dtexec command that works fine in a cmd batch file:dtexec /f %DTSPackagesDir%"\%SSISJob%" /Decrypt %DTSPassword% /CONN cm_sql_USTG;%cm_sql_USTG% /Reporting E /Set \Package.Variables[User::strDestinationFolderForFlatFile].Value;%TargetDir%\ /Set \Package.Variables[User::strExtractType].Value;%ExtractType% /Set \Package.Variables[User::strDayKey].Value;%ProcDate%I tried converting it for Powershell and ended up with this:dtexec /f "$cfgDTSPackagesDir\$DTSPackageName /Decrypt $cfgDTSPassword /CONN cm_sql_USTG;$cm_sql_USTG /Reporting E /Set \Package.Variables[User::strDestinationFolderForFlatFile].Value;$TargetDir\ /Set \Package.Variables[User::strExtractType].Value;$ExtractType /Set \Package.Variables[User::strDayKey].Value;$DayKey"The connection string is:Set-Variable -name cm_sql_ustg "Data Source=$cfgDBServer;Initial Catalog=$cfgDBName;Provider=SQLNCLI10.1;Integrated Security=SSPI;Auto Translate=False;Application Name=$AppName"; Write-Output "Connection String cm_sql_ustg: $cm_sql_ustg"I am either getting that the "system cannot find the file specified" and I know it is there, or if I make some changes, it stops on the connection string, not recognizing "Source" (as part of the "Data Source ...." string.Thanks for any help.
↧
Executing a simple Powershell command using SQL job
I am trying to delete sql backup files under UNC path from one server to another server. I am getting an error message - An object at the specified path \\PRPWD01V0131\L$\SQLBackup\Archive does not existScript -remove-item "\\PRPWD01V0131\L$\SQLBackup\Archive\*" -include *.bak -recurseI believe it's UNC path issue with credentials. Any help is appreciated!!Thanks!
↧
Multithread : Powershell - To execute .sql procedures in multiple servers
Hi ExpertsDoes anyone any implemented any such logic wherein it helps life in deploying multiple scripts in different servers in parallel using multi-thread?Thanks
↧
↧
Hi
Hey,I am trying to get the SQL Jobs information and its status through powershell.can anyone help me how to retrieve these results to html format wise.can any one give us the code
↧
Unique items from an array in order
How to get the unique list of Scriptfiles in the order they were added, ie,File2File1[code="plain"]$ScriptPath = "File2"$ServerList = ("ServerB", "ServerC")$DB = ("DB1", "DB2")$ScriptItems = @()foreach ($Server in $ServerList) { foreach ($Database in $DB) { $ScriptItem = New-Object System.Object $ScriptItem | Add-Member -type NoteProperty -name ScriptFile -Value $ScriptPath $ScriptItem | Add-Member -type NoteProperty -name Server -value $Server $ScriptItem | Add-Member -type NoteProperty -name Database -value $Database $ScriptItems += $ScriptItem } } $ScriptPath = "File1"$ServerList = ("ServerB", "ServerC")$DB = ("DB1", "DB2")foreach ($Server in $ServerList) { foreach ($Database in $DB) { $ScriptItem = New-Object System.Object $ScriptItem | Add-Member -type NoteProperty -name ScriptFile -Value $ScriptPath $ScriptItem | Add-Member -type NoteProperty -name Server -value $Server $ScriptItem | Add-Member -type NoteProperty -name Database -value $Database $ScriptItems += $ScriptItem } } $ScriptItems[/code]Thanks!
↧
SQL agent job failing with cannot open service control manager
Dear list,I am able to run get-services from powershell command window/ise but when I schedule the same script in SQL agent job and execute, it is failing with cannot open service control manager.Can anyone share your experience on this. ThanksKowsalya
↧