I'm new to PowerShell and am trying to query against my SQL server. I get the idea of creating a new-psdrive and then navigating to databases etc and have a line of code as
$dbs = (get-childitem
sqlserver:\sql\SERVER\INSTANCE\databases)
when I pipe the $dbs to a foreach, how would I get results of a collection of the database object? I am trying to read the extendedproperties of my test database.
This single query gives the results I want repeated for each database.
set-location
DRIVENAME:\databases\beagle_test\extendedproperties
get-childitem | select displayname,
value
any help very much appreciated.
I dont have SQL server handy to try this. Let me know the result
Set-Location DRIVENAME:\Databases
Get-ChildItem | % { Get-ChildItem $("$_.Name\extendedproperties") | Select DisplayName, Value }
Try this
Set-Location DRIVENAME:\Databases
Get-ChildItem | foreach-object { if (Test-Path $("$.Name\extendedproperties")) { Get-ChildItem $("$.Name\extendedproperties") | Select DisplayName, Value } }
The second line here is a single statement. What I am doing is to check if Extendedproperties exist and then get child item.
How about:
dir sqlserver:\sql\ServerName\InstanceName\Databases\*\ExtendedProperties\* |
select #{Name="Database";Expression={$_.Parent.Name}}, Name, Value
How about just:
dir SQLSERVER:\SQL\Server\Instance\databases\*\extendedproperties\* | % {select $_.displayname, $_.value}
so, many years later I am looking into my SO stats and see this old question and figure that my powershell skills have grown a little since 2010.
The use-case has long gone but I think what I was trying to achieve is this:
foreach ($db in $SMOServer.databases | Where-Object status -eq 'normal') {
$db.ExtendedProperties | Select-Object #{name = "DBName"; expression = {$db.Name}}, name, value
}
which gives results like this:
DBName Name Value
------ ---- -----
AdventureWorks2014 MS_Description AdventureWorks 2014 Sample OLTP Database
AdventureWorks2016 MS_Description AdventureWorks 2016 Sample OLTP Database
Related
I have a CSV file for help desk calls. The same ticket might have 1,2, or even 5 records based on the number of updates it has. (One field is different, all other fields are identical).
I want to take the mostly-duplicate records and create one record with the differences concatenated into it. Having programmed in the past, but being a newbie to PowerShell, I could use some help.
So, based on a previous question I asked, here's what I have so far. Assuming data like this:
ID, Date, Comment
345, 1/1/16, Moss has reported a fire in the I/T Room
345, 1/1/16, Never mind, he has sent an e-mail about it.
346, 1/2/16, Someone on the 5th floor is complaining about a man under her desk.
347, 2/1/16, Jen says she has broken the Internet.
347, 2/1/16, Douglas is very angry, we need a fix ASAP!
347, 2/1/16, Roy was playing a joke on her. Closing ticket.
I have the following code:
$FileList = Import-Csv "Call List.csv"
$incidents = $FileList | Group ID
foreach($group in $incidents)
{
# What goes here?
}
How do I take the comments from the 2nd, 3rd, etc. line in the group, concatenate it to the comment in the first, and write the file out?
The Group-Object produces an object with Name and Group, Group containing all the items in that group. You can extract them and create a new object using something like this:
$incidents = $FileList | Group-Object ID | % {
New-Object psobject -property #{
ID = $_.Name
Date = $_.Group[0].Date
Comment = ($_.Group | Select -Expandproperty Comment) -Join "`n"
}
}
(not tested as I am currently on a Mac)
I'd first get a list of the unique IDs, for example:
$Ids = $FileList | Select-Object -ExpandProperty Id -Unique
Then I'd look through the list of tickets and build up a "report" for each ID:
foreach($Id in $Ids){
# Get all incident logs for this Id:
$logs = $FileList | ?{$_.Id -eq $Id}
$report = ""
foreach($log in $logs){
$report += $log.Date + ": " + $log.Comment + "; "
}
# Now you can write the text out
$report | Out-File $Outfile -Append
}
Hope that gives you an idea.
I'm trying to make a PowerShell PSObject out of the output of a batch file.
I am calling the batch file like this, which gives me an object containing the output of the batch file:
function browseGateway {
& $migrationUtilityScript "browse" "-z" "$sourceGateway" "-showIds" "--hideProgress" |
select -Skip 1 |
Tee-Object -Variable gatewayItems |
Out-Null |
Sort-Object
Clear-Host
return $gatewayItems
}
The output looks like this:
folder 27fa3a0eb01caf5f1e31b6aeadd68d35 _Management and internal
folder 2ab9e48a1b83968c3844d93bc0919919 Mediclinic
folder 2c0a788b7f39a0415b423f14c0aa9e9c OAuth
folder a49ab6404138110fbd6c993cc9b87e46 customers
folder b015056834707a42a07bcbfbbeb0a6dd Users
folder b015056834707a42a07bcbfbbeb2ca34 Product Offerings
folder e001cfd0c1c1ffaa18e187b5e72fc718 OTK-3.2.00
policy 20a437bb3db29c1700a4f074773ffa41 DemoLogo
service 2ab9e48a1b83968c3844d93bc08ba780 kevinGMU
service 2ab9e48a1b83968c3844d93bc09596e9 testJJ
service 3afaef13bd772e8e25acfe85c4d101a7 Error
service 88f089fba30183fc4f18b7b7d05f5889 test
I want to put them into PowerShell Objects for further processing:
Column 1: Types
Column 2: IDs
Column 3: Names
I'm pretty sure you need to use New-Object for this, but I'm not entirely sure how to sort the information. Do I need to use a regex for each word and split it up further?
As you are effectively reading in a tab delimited file you don't need to use a RegEx, although you certainly could approach it that way. Once you have your raw input in a $items object you can use the following statements to convert it into an array of PSObject:
$items| ForEach-Object {
$properties = $_ -Split "`t"
New-Object PSObject -Property #{
Type = $properties[0].Trim();
ID = $properties[1].Trim();
Name = $properties[2].Trim();
}
}
Results in:
Name ID Type
---- -- ----
_Management and internal 27fa3a0eb01caf5f1e31b6aeadd68d35 folder
Mediclinic 2ab9e48a1b83968c3844d93bc0919919 folder
OAuth 2c0a788b7f39a0415b423f14c0aa9e9c folder
customers a49ab6404138110fbd6c993cc9b87e46 folder
Users b015056834707a42a07bcbfbbeb0a6dd folder
Product Offerings b015056834707a42a07bcbfbbeb2ca34 folder
OTK-3.2.00 e001cfd0c1c1ffaa18e187b5e72fc718 folder
DemoLogo 20a437bb3db29c1700a4f074773ffa41 policy
kevinGMU 2ab9e48a1b83968c3844d93bc08ba780 service
testJJ 2ab9e48a1b83968c3844d93bc09596e9 service
Error 3afaef13bd772e8e25acfe85c4d101a7 service
test 88f089fba30183fc4f18b7b7d05f5889 service
If you wanted to use a RegEx simply swap out the $_.Split for an equivalent $_ -Match <RegEx> and instead of referencing the $properties array you would be referencing the $matches variable. There is an exceptionally good run down of RegEx in PowerShell on Jouni Heikniemi's Blog.
You can do this quite easily with some -splits - regex is a tad bit overkill for something as simple as this.
$Content = (Invoke-WebRequest "http://pastebin.com/raw/591dk6Gj").Content
$FinalObj = $Content -split "`r`n" | % {
$Delimited = $_ -split "`t"
[PSCustomObject]#{
Col1 = $Delimited[0]
Col2 = $Delimited[1]
Col3 = $Delimited[2]
}
}
return $FinalObj
You end up with $FinalObj containing this:
Col1 Col2 Col3
---- ---- ----
folder 27fa3a0eb01caf5f1e31b6aeadd68d35 _Management and internal
folder 2ab9e48a1b83968c3844d93bc0919919 Mediclinic
folder 2c0a788b7f39a0415b423f14c0aa9e9c OAuth
folder a49ab6404138110fbd6c993cc9b87e46 customers
folder b015056834707a42a07bcbfbbeb0a6dd Users
folder b015056834707a42a07bcbfbbeb2ca34 Product Offerings
folder e001cfd0c1c1ffaa18e187b5e72fc718 OTK-3.2.00
policy 20a437bb3db29c1700a4f074773ffa41 DemoLogo
service 2ab9e48a1b83968c3844d93bc08ba780 kevinGMU
service 2ab9e48a1b83968c3844d93bc09596e9 testJJ
service 3afaef13bd772e8e25acfe85c4d101a7 Error
service 88f089fba30183fc4f18b7b7d05f5889 test
I'm trying to get all users that had been disabled in my domain and put it into a SQL Table. I'm trying to use SSIS to do that. Now that I can grab the right output and put it into a CSV file using this code:
Search-ADAccount -AccountDisabled -UsersOnly |
Select Name |
Export-CSV -Path C:\Users\hou\Downloads\Test.csv
But since I'm going to run the package in different servers and I couldn't have a fixed location to store the file and load into SQL Table. So either I'm going to use a variable in the Execute Process Task (where I run the Powershell script) to store the CSV file, or use SSIS to store the output directly in SQL table.
But I don't know neither of those. How can I do that?
Location should be define in the script, i.e:
$Path = Get-Location
"$Path\Test.csv"
#Option 1 just a Name
$Path = Get-Location ; Get-ChildItem C:\temp | Select-Object Name | Export-CSV -Path "$Path\Test.csv"
## Option 2 Name, with other property
$Path = Get-Location ; Get-ChildItem C:\temp | Select-Object Name,mode | Export-CSV -Path "$Path\Test.csv"
For one liner script , use the ";" to separate the commands.
I would suggest loading the data into SQL using PowerShell. There is a free PowerShell module from Microsoft called "sqlserver" that allows PowerShell to talk directly to SQL. You may already have it installed.
## Check if installed:
Get-InstalledModule -Name SqlServer
## If installed you can Update (you may need to run as local admin):
Update-Module -Name SqlServer
## If not installed (only need admin if you want all users to have access to this module):
Install-Module SqlServer
Once the module is installed there is a cmdlet called "Write-SqlTableData" to bulk copy data into SQL. The assumption is (1) the table already exists in SQL. (2) All the columns in the PowerShell Select match the order and datatype as they exist in the SQL table. In this case, there would be a SQL table with a single [Name] column. (3) You are using your AD credentials to access SQL, if not you will have to add credentials to the cmdlet.
The actual PowerShell code, update the variables in the quotes:
## Input Variables
$SqlServerName = ""
$SqlDatabaseName = ""
$SqlTableSchemaName = ""
$SqlTableName = ""
## Insert into SQL
Search-ADAccount -AccountDisabled -UsersOnly | Select-Object Name | Write-SqlTableData -ServerInstance $SqlServerName -DatabaseName $SqlDatabaseName -SchemaName $SqlTableSchemaName -TableName $SqlTableName -Force
As a side note, if you plan on doing a lot of PowerShell/SQL work, you may want to also install the "WFTools" module as it also has many additional SQL cmdlets.
Hope this helps.
I am sure there is an easy answer to this question, but I cannot find it anywhere. I would like to know how to parse the results of Get-Mailbox | select Alias into an array so each time I use the array it does not show the items as "#{Alias=username}".
I have tired this, but it seems to make the values not text:
$arrayname = Get-Mailbox | select Alias
I am sure this question has been asked before, but I cannot find it.
Essentially I would like to get the Alias' from the Get-Mailbox command into an array so that I can use the foreach cmdlet to get specific folder information from a user like so:
>> $aliases = Get-Mailbox | select Alias
>> Foreach ($username in $aliases) {Get-MailboxFolderStatistics -identity $username | select FolderPath | where {$_.FolderPath -like '*Deleted*'} | Export-CSV "C:\users\username\desktop\allusers-deletedfolder-postarchive.csv" -NoTypeInformation}
The cmdlet already produces an array, only that its elements are mailbox objects, not strings. Selecting the Alias property restricts the object properties, but still leaves you with an array of objects (hence the output you observed). You need to expand the property:
$arrayname = Get-Mailbox | select -Expand Alias
or echo it in a loop:
$arrayname = Get-Mailbox | % { $_.Alias }
Im trying to extract virtual network information for a VM using powershell, i tried using regular expression but for VM's with more than 1 NIC im unable to see output
Below is the output which i need..
PS C:\> get-vm sql.IAN01.Host | select -ExpandProperty virtualnetworkadapters | select virtualnetwork
VirtualNetwork
--------------
VirtualUplink
iSCSI1
iSCSI2
VirtualUplink
But when i try using regular expressions it does not give me an output, Network comes blank
PS C:\> Get-VM sql.IAN01.Host | Select #{Name="VMName";Expression={$_.name}},#{Name="Network
";Expression={#((get-vm $_.name | select -ExpandProperty virtualnetworkadapters).virtualnetwork)}}
VMName Network
------ -------
sql.IADPSQLHST1N01.Hosting
Can anyone please help me out!!
Try this:
Get-VM sql.IAN01.Host | Select-Object #{Name="VMName";Expression={$_.name}},#{Name='VirtualNetwork';e={$_.VirtualNetworkAdapters | Foreach-Object{$_.VirtualNetwork}}}