Is there any way linking GPO with ou using the ou canonical name? - windows

Is there a way i can link GPO with an OU by its canonical name?
New-Gplink -gpo <gponame> -target <CANONICAL NAME>
From what I've read we can use only the distinguished name. maybe there's a way around it?
Im using this to save all of them in a variable
$test=Get-ADOrganizationalUnit -Filter * -Properties CanonicalName | Select-Object -Property CanonicalName
now using a gui i open a window for the user to select an ou from there
foreach ($item in $test){
[void]$listbox.items.add($item)}
So now i can catch the user choise by using:
$catch = $listbox.selected.item
so if i now would like to link the gpo using
new-gplink -gpo <gponame> -target $catch
I will get an error.
Any help would be much appreciated!

Try it this way:
# Get the OU objects, which include the DN, plus the CN
$test = Get-ADOrganizationalUnit -Filter * -Properties CanonicalName
# Add the values to your listbox - there's an AddRange method for arrays
[void]$listbox.Items.AddRange($test.CanonicalName)
# Find the object that matches the currently selected Canonical Name
$CatchDN = $test | Where-Object { $_.CanonicalName -eq ($listbox.SelectedItem) }
# Because $CatchDN is an object, just link the GPO using the object.
New-GpLink -Name <gponame> -Target $CatchDN
No tested the above using forms, but it should work as intended.

Ok, using your own form code:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'GPO Connector V1.0'
$form.Size = '600,200'
$form.StartPosition = 'CenterScreen'
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = '10,120'
$button1.Size = '75,23'
$button1.Text = 'Link'
$button1.Anchor = 'Left,Bottom'
$button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $button1
$button2 = New-Object System.Windows.Forms.Button
$button2.Location = '90,120'
$button2.Size = '75,23'
$button2.Text = 'UnLink'
$button2.Anchor = 'Left,Bottom'
$Button2.Add_Click($Button2_Click)
$label = New-Object System.Windows.Forms.Label
$label.Location = '80,20'
$label.Size = '480,20'
$label.Text = 'SELECT GPO And Corresponding OU (ONLY WORKSTATION OU)'
$form.Controls.AddRange(#($label,$button1,$button2))
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = '10,40'
$listBox.Size = '260,80'
$listbox.Anchor = 'Top,Left,Bottom'
$listBox2 = New-Object System.Windows.Forms.ListBox
$listBox2.Location = '300,40'
$listBox2.Size = '260,80'
$listbox2.Anchor = 'Top,Left,Bottom,Right'
$form.Controls.AddRange(#($listBox,$listBox2))
$button1.Add_Click({
# Find the object that matches the currently selected Canonical Name
$CatchDN = $Script:OUlist | Where-Object { $_.CanonicalName -eq ($listbox2.SelectedItem) }
Write-Host "Selected CN object: $CatchDN"
# Because $CatchDN is an object, just link the GPO using the object.
$LinkedGP = $Script:GPOlist | Where-Object { $_.DisplayName -eq ($listBox.SelectedItem) }
Write-Host "Selected GPO: "$LinkedGP.DisplayName
$LinkedGP | New-GpLink -Target $CatchDN -WhatIf
})
# Show form first, then load data to lists
$form.Add_Shown({
# Retrieve GPO list
$Script:OUlist = Get-ADOrganizationalUnit -Filter * -Properties CanonicalName
# Add OU CN to listbox
$listbox2.Items.AddRange($Script:OUlist.CanonicalName)
$Script:GPOlist = Get-GPO -All # list of GPO
$listbox.Items.AddRange($Script:GPOlist.DisplayName)
})
$form.Topmost = $true
$result = $form.ShowDialog()
$form.Dispose()

Related

Powershell - How to export to CSV file a query with Oracle Data Provider for .NET (ODP.NET)

In Powershell, I want to export the results of a query ODP.NET to a CSV file
This is my code:
$connection = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionString)
$connection.open()
$cmd = New-Object Oracle.ManagedDataAccess.Client.OracleCommand -ArgumentList $query
$cmd.Connection = $connection
$reader = $cmd.ExecuteReader()
This code is working with the correct values of $connectionString and $query variables. If I add this code I can read the correct result of my query:
while ($reader.Read()) {
$col1 = $reader["Col1"]
$col2 = $reader["Col2"]
$col3 = $reader["Col3"]
#Write-Host $col1, $col2, $col3
}
Those 3 columns are an example, in my real case I have many more. Then I want to export directly to a CSV file, something like this:
XXXXXXX | export-csv -Delimiter ";" -Path "E:\export.csv"
How can I do this? The expected result is a CSV file similar than this:
"4581";"6";26867;"191057";"BH02 - 26867 - ";"30/05/2019";"";"2040";1991,04;"2040";2,4;"00";"";348;"";"1";"1";"";"";"BRL";2040;"";1
"4581";"4";28313;"747990";"BH02 - 28313 - ";"30/05/2019";"";"140";137,13;"140";2,05;"00";"";459;"";"1";"1";"";"";"BRL";140;"";1
"4581";"1";28316;"881411";"BH02 - 28316 - ";"30/05/2019";"";"140";137,13;"140";2,05;"00";"";460;"";"1";"1";"";"";"BRL";140;"";1
"4581";"1";;"878676";"BH02 - - 275650/PF19";"28/05/2019";"";"103";100,8885;"103";2,05;"00";"";305;"";"1";"1";"";"";"BRL";103;"";1
"4581";"6";28168;"006778";"BH02 - 28168 - 275714/PF19";"30/05/2019";"";"848";828,92;"848";2,25;"00";"";429;"";"1";"1";"";"";"BRL";848;"";1
"4581";"3";29080;"641559";"BH02 - 29080 - ";"30/05/2019";"";"3424,14";3338,5365;"3424,14";2,5;"00";"";488;"";"1";"1";"";"";"BRL";3424,14;"";1
"4581";"4";;"602483";"BH02 - - 23443";"28/05/2019";"";"157";153,7815;"157";2,05;"00";"";329;"";"1";"1";"";"";"BRL";157;"";1
What is the raw output of this Oracle command?
If it is a list, then use -Join and use the Csv cmdlets to export to convert to a csv file.
This also would indicate that you are new or never used PowerShell before since csv use case is a daily thing. That's OK, but jump on YouTube, MSDN Channel9 and go through the videos on PowerShell, beginning/intermediate/advanced, PowerShell and Databases, PowerShell and Csv files and get ramped up to limit/avoid, misconceptions, frustrations, bad habits, etc.
It should be straightforward as doing something like this ( of course don't put passwords in scripts)...
Add-Type -Path 'C:\oracle\instantclient_10_2\odp.net\managed\common\Oracle.ManagedDataAccess.dll'
$username = "USERID"
$password = "Password"
$datasource = "HOST:PORT/Instance"
$connectionString = "User Id = $username;Password = $password;Data Source = $datasource"
$query = "SELECT DATA1, DATA2, DATA3, DATA4, DATA5, DATA6, DATA7, DATA8
FROM TABLE WHERE NOT REGEXP_LIKE (EMAIL_ID, '#domain.com','i') order by DATA2"
$connection = New-Object Oracle.ManagedDataAccess.Client.OracleConnection("$connectionString")
$connection.open()
$command = New-Object Oracle.ManagedDataAccess.Client.OracleCommand
$command.Connection = $connection
$command.CommandText = $query
$ds = New-Object system.Data.DataSet
$da = New-Object Oracle.ManagedDataAccess.Client.OracleDataAdapter($command)
[void]$da.fill($ds)
return $ds.Tables[0] |
SELECT DATA1, DATA2, DATA3, DATA4, DATA5, DATA6, DATA7, DATA8 |
Export-CSV "C:\test.csv" -NoTypeInformation
$connection.Close()

Can't export more than 2 lines to the csv

[void][System.Reflection.Assembly]::LoadWithPartialName("Oracle.DataAccess")
$username = "USER"
$password = "PW"
$datasource = "XXX.XXX.XXX.XXX:YYYY/NODE1"
function getConnectionString()
{
#Below is a one line code
$connectionstring = "Data Source=$datasource;User Id=$($username); Password=$($password)"
return $connectionstring
}
$query = get-content "C:\Test.sql";
$connectionstring = getConnectionString
$DataTable = New-Object System.Data.DataTable
$command = New-Object Oracle.DataAccess.Client.OracleCommand($query,$connectionstring)
$dataAdapter = New-Object Oracle.DataAccess.Client.OracleDataAdapter($command)
[void]$dataAdapter.Fill($DataTable)
#To display data on console
$DataTable | format-table -auto
#To export to csv
$DataTable | Export-Csv "C:\Test.csv" -notype
The "Test.sql contains:
SELECT ACCOUNTNUMBER, ACCOUNTID, USERID FROM account
WHERE ACCOUNTNUMBERLIKE '%0011111%'
That quesy brings back 3 rows.
Issue at hand: My powershell script only gives me the first 2 rows (1 being the headers, the other being the first data row)
Any advice?

How to get specific number of rows from Excel in powershell

I need to get first 35 rows (including empty rows) from column A to a variable.
I looked in the internet but I cannot find the answer anywhere. $data = $worksheet.Range("A1:A35").text returns only cell A1. I tried with Cell.item etc. but with no success. Does anyone know how to extract cell range A1:A35 from excel into variable and save it to the text file? Thanks in advance.
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.workbooks.open("*PATH_TO_THE_FILE*.xlsx")
$worksheet = $workbook.sheets.item("MatrixFill")
$data = $worksheet.Range("A1:A35").text
$excel.Quit()
the text property you are trying to access is actually an object, so you have to treat it as such. Also for ranges you will need to use a , instead of :. Below will give you what you need. It worked for me when I tested it.
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.workbooks.open("*PATH_TO_THE_FILE*.xlsx")
$worksheet = $workbook.sheets.item("MatrixFill")
$worksheet.Range("A1","A35") | select -expand text |out-file "textfilename.txt"
$excel.Quit()
or
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.workbooks.open("*PATH_TO_THE_FILE*.xlsx")
$worksheet = $workbook.sheets.item("MatrixFill")
$data = $worksheet.Range("A1","A35") | select -expand text
$data | out-file "textfilename.txt"
$excel.Quit()

formatting csv files and powershell

Ok so we have a manual process that runs through PL/SQL Developer to run a query and then export to csv.
I am trying to automate that process using powershell since we are working in a windows environment.
I have created two files that seems to be exact duplicates from the automated and manual process but they don't work the same so I assume I am missing some hidden characters but I can't find them or figure out how to remove them.
The most obvious example of them working differently is opening them in excel. The manual file opens in excel automatically putting each column in it's own seperate column. The automated file instead puts everything into one column.
Can anybody shed some light? I am hoping that by resolving this or at least getting some info will help with the bigger problem of it not processing correctly.
Thanks.
ex one column
"rownum","year","month","batch","facility","transfer_facility","trans_dt","meter","ticket","trans_product","trans","shipper","customer","supplier","broker","origin","destination","quantity"
ex seperate column
"","ROWNUM","RPT_YR","RPT_MO","BATCH_NBR","FACILITY_CD","TRANSFER_FACILITY_CD","TRANS_DT","METER_NBR","TKT_NBR","TRANS_PRODUCT_CD","TRANS_CD","SHIPPER_CD","CUSTOMER_NBR","SUPPLIER_NBR","BROKER_CD","ORIGIN_CD","DESTINATION_CD","NET_QTY"
$connectionstring = "Data Source=database;User Id=user;Password=password"
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionstring)
$command = New-Object System.Data.OracleClient.OracleCommand($query, $connection)
$connection.Open()
Write-Host -ForegroundColor Black " Opening Oracle Connection"
Start-Sleep -Seconds 2
#Getting data from oracle
Write-Host
Write-Host -ForegroundColor Black "Getting data from Oracle"
$Oracle_data=$command.ExecuteReader()
Start-Sleep -Seconds 2
if ($Oracle_data.read()){
Write-Host -ForegroundColor Green "Connection Success"
while ($Oracle_data.read()) {
#Variables for recordset
$rownum = $Oracle_data.GetDecimal(0)
$rpt_yr = $Oracle_data.GetDecimal(1)
$rpt_mo = $Oracle_data.GetDecimal(2)
$batch_nbr = $Oracle_data.GetString(3)
$facility_cd = $Oracle_data.GetString(4)
$transfer_facility_cd = $Oracle_data.GetString(5)
$trans_dt = $Oracle_data.GetDateTime(6)
$meter_nbr = $Oracle_data.GetString(7)
$tkt_nbr = $Oracle_data.GetString(8)
$trans_product_cd = $Oracle_data.GetString(9)
$trans_cd = $Oracle_data.GetString(10)
$shipper_cd = $Oracle_data.GetString(11)
$customer_nbr = $Oracle_data.GetString(12)
$supplier_nbr = $Oracle_data.GetString(13)
$broker_cd = $Oracle_data.GetString(14)
$origin_cd = $Oracle_data.GetString(15)
$destination_cd = $Oracle_data.GetString(16)
$net_qty = $Oracle_data.GetDecimal(17)
#Define new file
$filename = "Pipeline" #Get-Date -UFormat "%b%Y"
$filename = $filename + ".csv"
$fileLocation = $newdir + "\" + $filename
$fileExists = Test-Path $fileLocation
#Create object to hold record
$obj = new-object psobject -prop #{
rownum = $rownum
year = $rpt_yr
month = $rpt_mo
batch = $batch_nbr
facility = $facility_cd
transfer_facility = $transfer_facility_cd
trans_dt = $trans_dt
meter = $meter_nbr
ticket = $tkt_nbr
trans_product = $trans_product_cd
trans = $trans_cd
shipper = $shipper_cd
customer = $customer_nbr
supplier = $supplier_nbr
broker = $broker_cd
origin = $origin_cd
destination = $destination_cd
quantity = $net_qty
}
$records += $obj
}
}else {
Write-Host -ForegroundColor Red " Connection Failed"
}
#Write records to file with headers
$records | Select-Object rownum,year,month,batch,facility,transfer_facility,trans_dt,meter,ticket,trans_product,trans,shipper,customer,supplier,broker,origin,destination,quantity |
ConvertTo-Csv |
Select -Skip 1|
Out-File $fileLocation
Why are you skipping the first row(usually the headers)? Also, try using Export-CSV instead:
#Write records to file with headers
$records | Select-Object rownum, year, month, batch, facility, transfer_facility, trans_dt, meter, ticket, trans_product, trans, shipper, customer, supplier, broker, origin, destination, quantity |
Export-Csv $fileLocation -NoTypeInformation

Powershell - GUI and menustrips

I've tried looking on the internet but can't find anything on this.
I've built a powershell GUI app but want to add a menu bar with different menu items to it.
is this possible? has anyone done it (with examples)?
Thanks
Here is a sample using Windows Forms. I design it with Visual studio and then transform it to PowerSell using Convert-Form a free tool written by Laurent Dardenne from the nice french PowerShell site PowerShelll-Scripting.com
# Load external assemblies
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$MS_Main = new-object System.Windows.Forms.MenuStrip
$fileToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
$openToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
$editionToolStripMenuItem = new-object System.Windows.Forms.ToolStripMenuItem
#
# MS_Main
#
$MS_Main.Items.AddRange(#(
$fileToolStripMenuItem,
$editionToolStripMenuItem))
$MS_Main.Location = new-object System.Drawing.Point(0, 0)
$MS_Main.Name = "MS_Main"
$MS_Main.Size = new-object System.Drawing.Size(354, 24)
$MS_Main.TabIndex = 0
$MS_Main.Text = "menuStrip1"
#
# fileToolStripMenuItem
#
$fileToolStripMenuItem.DropDownItems.AddRange(#(
$openToolStripMenuItem))
$fileToolStripMenuItem.Name = "fileToolStripMenuItem"
$fileToolStripMenuItem.Size = new-object System.Drawing.Size(35, 20)
$fileToolStripMenuItem.Text = "&File"
#
# openToolStripMenuItem
#
$openToolStripMenuItem.Name = "openToolStripMenuItem"
$openToolStripMenuItem.Size = new-object System.Drawing.Size(152, 22)
$openToolStripMenuItem.Text = "&Open"
function OnClick_openToolStripMenuItem($Sender,$e){
[void][System.Windows.Forms.MessageBox]::Show("Event openToolStripMenuItem.Add_Click is not implemented.")
}
$openToolStripMenuItem.Add_Click( { OnClick_openToolStripMenuItem $openToolStripMenuItem $EventArgs} )
#
# editionToolStripMenuItem
#
$editionToolStripMenuItem.Name = "editionToolStripMenuItem"
$editionToolStripMenuItem.Size = new-object System.Drawing.Size(51, 20)
$editionToolStripMenuItem.Text = "&Edition"
#
$MenuForm = new-object System.Windows.Forms.form
#
$MenuForm.ClientSize = new-object System.Drawing.Size(354, 141)
$MenuForm.Controls.Add($MS_Main)
$MenuForm.MainMenuStrip = $MS_Main
$MenuForm.Name = "MenuForm"
$MenuForm.Text = "I\'ve got a menu"
function OnFormClosing_MenuForm($Sender,$e){
# $this represent sender (object)
# $_ represent e (eventarg)
# Allow closing
($_).Cancel= $False
}
$MenuForm.Add_FormClosing( { OnFormClosing_MenuForm $MenuForm $EventArgs} )
$MenuForm.Add_Shown({$MenuForm.Activate()})
$MenuForm.ShowDialog()
#Free ressources
$MenuForm.Dispose()

Resources