I'm trying to work with this code that I found. It says it is changing the password to the account I specify, but when I try to log into that laptop with that local account password was not changed.
I need to be able to change the password to the local user account remotely to 30 + laptops without logging into each of them to do so.
Any ideas why this is not working?
$erroractionpreference = "SilentlyContinue"
$a = New-Object -comobject Excel.Application
$a.visible = $True
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
$c.Cells.Item(1,1) = "Machine Name"
$c.Cells.Item(1,2) = "Password Changed"
$c.Cells.Item(1,3) = "Report Time Stamp"
$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True
$intRow = 2
foreach ($strComputer in get-content C:\MachineList.Txt)
{
$c.Cells.Item($intRow,1) = $strComputer.ToUpper()
# Using .NET method to ping test the servers – This is very cool!
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($strComputer)
if($Reply.status -eq "success")
{
# This is the Key Part
$admin=[adsi]("WinNT://" + $strComputer + "/LocalUser, user")
$admin.psbase.invoke("SetPassword", "Password")
$admin.psbase.CommitChanges()
$pwage = $admin.passwordage
If($pwage.value -ne "0")
{
$c.Cells.Item($intRow,2).Interior.ColorIndex = 4
$c.Cells.Item($intRow,2) = "Yes"
}
Else
{
$c.Cells.Item($intRow,2).Interior.ColorIndex = 3
$c.Cells.Item($intRow,2) = "No"
}
}
Else
{
$c.Cells.Item($intRow,2).Interior.ColorIndex = 3
$c.Cells.Item($intRow,2) = "Not Pingable"
}
$c.Cells.Item($intRow,3) = Get-Date
$Reply = ""
$pwage = ""
$intRow = $intRow + 1
}
$d.EntireColumn.AutoFit()
Related
I'm new with powershell and i would like to use a loop to ping several Printers on my network.
My problem is : once i'm in the loop of pinging , i can't go out of the loop ...
I tried several things from google but without success ( start-stop , Timer ) . Does anybody have any idea?
Here is the code :
$BtnStartPingClicked = {
if ($LblFileSelectPing.Text -eq "*.txt") {
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OK
$MessageIcon = [System.Windows.MessageBoxImage]::Error
$MessageBody = "Please select a list of printer first"
$MessageTitle = "Error"
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
Write-Host "Your choice is $Result"
}
else {
do {
$IPList = Get-Content ($LblFileSelectPing.Text)
$snmp = New-Object -ComObject olePrn.OleSNMP
$ping = New-Object System.Net.NetworkInformation.Ping
$i = 11
$j = 1
foreach ($Printer in $IPList) {
try {
$result = $ping.Send($Printer)
} catch {
$result = $null
}
if ($result.Status -eq 'Success') {
$((Get-Variable -name ("GBMachine"+$j+"Ping")).value).Visible = $True
$j++
test-Connection -ComputerName $Printer -Count 1 -Quiet
$printerip = $result.Address.ToString()
# OPEN SNMP CONNECTION TO PRINTER
$snmp.open($Printer, 'public', 2, 3000)
# MODEL
try {
$model = $snmp.Get('.1.3.6.1.2.1.25.3.2.1.3.1')
} catch {
$model = $null
}
# Serial
try {
$serial = $snmp.Get('.1.3.6.1.4.1.1602.1.2.1.8.1.3.1.1').toupper()
} catch {
$Dns = $null
}
# progress
$TBMonitoringPing.SelectionColor = "green"
$TBMonitoringPing.AppendText("$Printer is Pinging")
$TBMonitoringPing.AppendText("`n")
$mac = (arp -a $Printer | Select-String '([0-9a-f]{2}-){5}[0-9a-f]{2}').Matches.Value
# OPEN SNMP CONNECTION TO PRINTER
$((Get-Variable -name ('LblMach' + $i)).value).Text = "IP : $Printerip"
$i++
$((Get-Variable -name ('LblMach' + $i)).value).Text = "Model : $Model"
$i++
$((Get-Variable -name ('LblMach' + $i)).value).Text = "MAC : $mac"
$i++
$((Get-Variable -name ('LblMach' + $i)).value).Text = "Serial : $serial"
$TBAnswerMachine.AppendText("$Model")
$TBAnswerMachine.AppendText("`n")
$TBAnswerMachine.AppendText("$Printer - $Serial")
$TBAnswerMachine.AppendText("`n")
$TBAnswerMachine.AppendText("$Mac")
$TBAnswerMachine.AppendText("`n")
$TBAnswerMachine.AppendText("`n")
Get-Content ($LblFileSelectPing.Text) | Where-Object {$_ -notmatch $Printer} | Set-Content ("C:\_canonsoftware\out.txt")
$i = $i+7
$snmp.Close()
Start-Sleep -milliseconds 1000 # Take a breather!
}
else {
$TBMonitoringPing.selectioncolor = "red"
$TBMonitoringPing.AppendText("$Printer not pinging")
$TBMonitoringPing.AppendText("`n")
Start-Sleep -milliseconds 1000 # Take a breather!
}
}
$LblFileSelectPing.Text = "C:\_canonsoftware\out.txt"
} until($infinity)
}
}
thanks for your answers...
1 - part of my object are indeed not declared in the code because they are in my other PS1 file....
2 - I do a do until infinity because i don't want to stop the code before i decide it...
3 - I didn't explain my problem correctly ( excuse my poor english ) ... i would like to be able to go out of the loop do until at the moment i click on a stop button ... but apprently the windows doens't respond while in the loop ... i have to stop the script with powershell ... which is annoying because i'd like to make an executable with it ... and not have to go out of my program ...
thank you for your ideas
I use Dynamic Labels.
I need to know on which dynamic label right click was pressed.
When I double click the server name it writes the label I pressed.
But how can I get the label I pressed in ContextMenuStrip?
This is the relevant part of my script:
$contextMenuStrip1 = New-Object System.Windows.Forms.ContextMenuStrip
$Menu1a = $contextMenuStrip1.Items.Add("Item 1")
$Menu1a.add_Click(
{
param($Sender,$EventArgs) write-host $Sender.name
Write-Host $this.Text
})
$Menu1b = $contextMenuStrip1.Items.Add("Item 2")
$Menu1b.add_Click({Write-Host $this.Text})
$Global:DynLblElemCol1 = foreach($Server in $Servers)
{
$Col = New-Object System.Windows.Forms.Label
$Col.Size = New-Object System.Drawing.Size(100, 18)
$col.TextAlign = "MiddleCenter"
$col.Text = $Server
$Col.Name = $Server
$Col.Add_DoubleClick({param($Sender,$EventArgs) write-host $Sender.Text})
$Col.ContextMenuStrip = $contextMenuStrip1
$Col
}
foreach($item in $DynLblElemCol1)
{
$item.Location = New-Object System.Drawing.Point(80, $CheckBoxCounter)
$Form1.Controls.Add($item)
$CheckBoxCounter = $item.Bottom + $LineSpace # increment our counter
}
}
I need a script to change the IP address on all Windows 7 pro X64 PC.
If the first 2 match 192.168 then change
If IP already change then ignore and move onto next ip/PC.
Some PC will have more then 1 port.
I have try this which works but when you run it next time it keep adding the ip address.
$site = #{
Site1 = 2;
site2 = 3;
}
$site = $site.Values
$oldRange = "192.168."
foreach ($s in $site) {
$ipAddress = $oldRange + $s + ".*"
$printer = wmic path win32_tcpipprinterport get hostaddress
$printer = $printer.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
if ($printer) {
foreach ($p in $printer) {
$OldIP = $p
$OldIP2 = $OldIP -replace $oldRange, ""
$old = [bool]($OldIP -as [IPAddress])
if ($old) {
$NewIP = "172.15.$OldIP2"
$NewIP
wmic path win32_tcpipprinterport where "hostaddress = '$OldIP'" set hostaddress="$NewIP"
}
}
} else {
Write-Host "None Found"
}
}
This is what I have done.
if anyone has better solution then please let me know
Clear-Host
$site = #{
Site1=2;
Site2=3;
}
$site =$site.Values
$oldRange = "172.11."
foreach($s in $site)
{
$printer = wmic path win32_tcpipprinterport get hostaddress
$printer=$printer.Split('',[System.StringSplitOptions]::RemoveEmptyEntries)
if($printer)
{
foreach ($p in $printer)
{
$OldIP = $p
$OldIP2 = $OldIP -replace $oldRange, ""
$old = [bool]($OldIP -as [IPAddress])
if($old)
{
$sep = $OldIP.lastindexof(".")
$network = $OldIP.substring(0,$sep)
if("10.11."+$s -ne $network)
{
$NewIP = "10.11.$OldIP2"
$NewIP
wmic path win32_tcpipprinterport where "hostaddress = '$OldIP'" set hostaddress="$NewIP"
}else{
Write-Host "IP Already Changed"
}
}else
{
Write-Host "Not a IP address"
}
}
}else{
Write-Host "None Found"
}
}
I am trying to write a script which displays Active Directory users which are expired and disabled in a listview. I want the listview to group the accounts by disabled and expired accounts. When I run the script in the ISE, it displays properly:
When I run the script in a normal PowerShell prompt, grouping doesn't work:
I'm running PowerShell 5 on Windows 10 1703. This is my code, what have I done wrong?
Function ViewAllInactiveUsers {
$ViewAllInactiveUsersForm = New-Object system.Windows.Forms.Form
$ViewAllInactiveUsersForm.Text = "View All Inactive Users"
$ViewAllInactiveUsersForm.TopMost = $false
$ViewAllInactiveUsersForm.Width = 900
$ViewAllInactiveUsersForm.Height = 700
$ViewAllInactiveUsersForm.MinimizeBox = $false
$ViewAllInactiveUsersForm.MaximizeBox = $false
$ViewAllInactiveUsersForm.FormBorderStyle = "FixedDialog"
$InactiveListView_DoubleClick={
$InactiveUserTextBox.text = $InactiveListView.SelectedItems[0].SubItems[0].Text
}
#Define Header
$InactiveImage1 = [System.Drawing.Image]::FromFile("c:\Users\i.north\Documents\CWP\habs.png")
$InactiveImage = New-Object System.Windows.Forms.PictureBox
$InactiveImage.Width = $InactiveImage1.Size.Width
$InactiveImage.Height = $InactiveImage1.Size.Height
$InactiveImage.location = New-Object System.Drawing.Size(20,0)
$InactiveImage.image = $InactiveImage1
$ViewAllInactiveUsersForm.Controls.Add($InactiveImage)
$InactiveTitleFont = New-Object System.Drawing.Font("Calibri",20,[System.drawing.fontstyle]::Regular)
$InactiveTitleText = New-Object System.Windows.Forms.Label
$InactiveTitleText.Font = $TitleFont
$InactiveTitleText.Text = "View All Inactive Guests"
$InactiveTitleText.Location = New-Object System.Drawing.Size(330,20)
$InactiveTitleText.Size = New-Object System.Drawing.Size(400,100)
$ViewAllInactiveUsersForm.Controls.Add($InactiveTitleText)
#List box with Inactive users in
$InactiveListView = New-Object system.windows.Forms.ListView
$InactiveListView.Width = 840
$InactiveListView.Height = 300
$InactiveListView.location = new-object system.drawing.point(20,139)
$InactiveListView.View = [System.Windows.Forms.View]::Details
$InactiveListView.HeaderStyle = "nonclickable"
$InactiveListView.FullRowSelect = $true
$InactiveListView.add_SelectedIndexChanged({
If ($InactiveListView.Items.Count -gt 0)
{
$InactiveExtendButton.Enabled = $true
$InactiveDisableButton.Enabled = $true
$InactiveResetButton.Enabled = $true
$InactiveUserTextBox.Enabled = $true
$InactiveDurationBox.Enabled = $true
$InactiveUserTextBox.BackColor = "#ffffff"
}
Else
{
$InactiveExtendButton.Enabled = $false
$InactiveDisableButton.Enabled = $false
$InactiveResetButton.Enabled = $false
}
})
$InactiveListView.add_Click({$InactiveUserTextBox.text = $InactiveListView.SelectedItems[0].SubItems[0].Text})
$ViewAllInactiveUsersForm.controls.Add($InactiveListView)
$InactiveListView.Columns.Add("Guest User Name", -2)
$InactiveListView.Columns.Add("Guest Forename", -2)
$InactiveListView.Columns.Add("Guest Surname", -2)
$InactiveListView.Columns.Add("Guest Company", 250)
$InactiveListView.Columns.Add("Guest Sponsor", -2)
$InactiveListView.Columns.Add("Account Status", -2 )
$InactiveListView.Columns.Add("Account Expiry Time", -2)
$InactiveListViewDisabledGroup = New-Object System.Windows.Forms.ListViewGroup
$InactiveListViewDisabledGroup.Header = "Disabled Accounts"
$InactiveListView.Groups.Add($InactiveListViewDisabledGroup)
$InactiveListViewExpiredGroup = New-Object System.Windows.Forms.ListViewGroup
$InactiveListViewExpiredGroup.Header = "Expired Accounts"
$InactiveListView.Groups.Add($InactiveListViewExpiredGroup)
$InactiveListView.ShowGroups = $true
#Get all Inactive guest AD users and put them into the list view
$GuestUsersExpired = get-aduser -filter {enabled -eq $true} -searchbase "ou=Guest Wifi Users,ou=Guest Wifi,dc=hahc,dc=internal" -Properties displayname,company,mail,officephone,description,office,accountexpires,AccountExpirationDate | Where-Object { $_.AccountExpirationDate -ne $null -and $_.AccountExpirationDate -lt (Get-Date) }
$GuestUsersExpired | ForEach-Object {
$Sponsor = $_.description
$SponsorSplit = $Sponsor.IndexOf("-")
$InactiveListViewItem = New-Object System.Windows.Forms.ListViewItem($_.SamAccountName)
$InactiveListViewItem.SubItems.Add($_.GivenName)
$InactiveListViewItem.SubItems.Add($_.Surname)
$InactiveListViewItem.SubItems.Add($_.Company)
$InactiveListViewItem.SubItems.Add($Sponsor.substring($SponsorSplit+2))
$InactiveListViewItem.SubItems.Add("Expired")
$InactiveListViewItem.SubItems.Add("$(Get-Date([datetime]::FromFileTime($_.AccountExpires)) -Format "ddd d MMM yyyy, HH:mm")")
$InactiveListViewItem.Group = $InactiveListViewExpiredGroup
$InactiveListView.Items.Add($InactiveListViewItem)
}
$GuestUsersDisabled = get-aduser -filter {enabled -eq $false} -searchbase "ou=Guest Wifi Users,ou=Guest Wifi,dc=hahc,dc=internal" -Properties displayname,company,mail,officephone,description,office,accountexpires,AccountExpirationDate
$GuestUsersDisabled | ForEach-Object {
$Sponsor = $_.description
$SponsorSplit = $Sponsor.IndexOf("-")
$InactiveListViewItem = New-Object System.Windows.Forms.ListViewItem($_.SamAccountName)
$InactiveListViewItem.SubItems.Add($_.GivenName)
$InactiveListViewItem.SubItems.Add($_.Surname)
$InactiveListViewItem.SubItems.Add($_.Company)
$InactiveListViewItem.SubItems.Add($Sponsor.substring($SponsorSplit+2))
$InactiveListViewItem.SubItems.Add("Disabled")
$InactiveListViewItem.SubItems.Add("$(Get-Date([datetime]::FromFileTime($_.AccountExpires)) -Format "ddd d MMM yyyy, HH:mm")")
$InactiveListViewItem.Group = $InactiveListViewDisabledGroup
$InactiveListView.Items.Add($InactiveListViewItem)
}
#Extend/Reset/Delete User area
#Put it all into a groupbox, make it look a little neater
$InactiveUserAreaGroupBox = New-Object System.Windows.Forms.GroupBox
$InactiveUserAreaGroupBox.Location = New-Object System.Drawing.Size(20,460)
$InactiveUserAreaGroupBox.Height = 135
$InactiveUserAreaGroupBox.Width = 840
$InactiveUserAreaGroupBox.Text = "User Control Area"
$ViewAllInactiveUsersForm.Controls.Add($InactiveUserAreaGroupBox)
#Label
$InactiveUserLabel = New-Object System.Windows.Forms.Label
$InactiveUserLabel.Location = New-Object System.Drawing.Size(20,20)
$InactiveUserLabel.Size = New-Object System.Drawing.Size(110,50)
$InactiveUserLabel.Text = "Guest UserName"
$InactiveUserAreaGroupBox.Controls.Add($InactiveUserLabel)
#TextBox
$InactiveUserTextBox = New-Object System.Windows.Forms.TextBox
$InactiveUserTextBox.Location = New-Object System.Drawing.Size(130,20)
$InactiveUserTextBox.Size = New-Object System.Drawing.Size(200,50)
$InactiveUserTextBox.Enabled = $false
$InactiveUserTextBox.ReadOnly = $true
$InactiveUserAreaGroupBox.Controls.Add($InactiveUserTextBox)
#disable user button
$InactiveDisableButton = New-Object System.Windows.Forms.Button
$InactiveDisableButton.Text = "Disable Guest Account"
$InactiveDisableButton.Width = 150
$InactiveDisableButton.Height = 40
$InactiveDisableButton.Location = New-Object System.Drawing.Point(670,70)
$InactiveDisableButton.Enabled = $false
$InactiveDisableButton.add_click({
Get-ADUser -Filter {name -eq $InactiveUserTextBox.Text} | Set-ADUser -Enabled $false
[System.Windows.Forms.MessageBox]::Show("User $($InactiveUserTextBox.Text) has been disabled")
$InactiveListView.Items.Remove($InactiveListView.SelectedItems[0])
})
$InactiveUserAreaGroupBox.Controls.Add($InactiveDisableButton)
#reset password button
$InactiveResetButton = New-Object System.Windows.Forms.Button
$InactiveResetButton.Text = "Reset Guest Password"
$InactiveResetButton.Width = 150
$InactiveResetButton.Height = 40
$InactiveResetButton.Location = New-Object System.Drawing.Point(505,70)
$InactiveResetButton.Enabled = $false
$InactiveResetButton.add_click({
$Password = GenerateRandomishPassword
$PasswordSecure = $Password | ConvertTo-SecureString -AsPlainText -Force
$UserToCreate = $InactiveUserTextBox.Text
Get-ADUser -Filter {name -eq $InactiveUserTextBox.Text } | Set-ADAccountPassword -NewPassword $PasswordSecure
UserCreationResultsForm
})
$InactiveUserAreaGroupBox.Controls.Add($InactiveResetButton)
#extend combobox
$InactiveDurationLabel = New-Object System.Windows.Forms.Label
$InactiveDurationLabel.Location = New-Object System.Drawing.Size(340,20)
$InactiveDurationLabel.Size = New-Object System.Drawing.Size(150,40)
$InactiveDurationLabel.Text = "Change Expiry Time to this amount of time from now:"
$InactiveUserAreaGroupBox.Controls.Add($InactiveDurationLabel)
$InactiveDurationBox = New-Object System.Windows.Forms.ComboBox
$InactiveDurationBox.Location = New-Object System.Drawing.Size(505,20)
$InactiveDurationBox.Size = New-Object System.Drawing.Size(150,40)
$InactiveDurationBox.Text = "8 Hours"
$InactiveDurationBox.Enabled = $false
$InactiveDurationBox.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
$InactiveUserAreaGroupBox.Controls.Add($InactiveDurationBox)
$InactiveDurationOptions=#("1 Hour","4 Hours","8 Hours","1 Day","1 Week","1 Month")
Foreach ($InactiveDuration in $InactiveDurationOptions) {
[void] $InactiveDurationBox.items.add($InactiveDuration)
}
#extend button
$InactiveExtendButton = New-Object System.Windows.Forms.Button
$InactiveExtendButton.Text = "Extend Guest Account Expiry Time"
$InactiveExtendButton.Width = 150
$InactiveExtendButton.Height = 40
$InactiveExtendButton.Location = New-Object System.Drawing.Point(670,20)
$InactiveExtendButton.Enabled = $false
$InactiveExtendButton.add_click({
$DurationExpiryTime = $InactiveDurationBox.Text
switch ($DurationExpiryTime) {
"1 Hour" { $TS = New-TimeSpan -Hours 1 }
"4 Hours" { $TS = New-TimeSpan -Hours 4 }
"8 Hours" { $TS = New-TimeSpan -Hours 8 }
"1 Day" { $TS = New-TimeSpan -Days 1 }
"1 Week" { $TS = New-TimeSpan -Days 7 }
"1 Month" { $TS = New-TimeSpan -Days 28 }
Default { $TS = New-TimeSpan -hours 8 }
}
Get-ADUser -filter {name -eq $InactiveUserTextBox.Text } | Set-ADAccountExpiration -TimeSpan $TS
[System.Windows.Forms.MessageBox]::Show("Expiration extended by $($InactiveDurationBox.Text)")
})
$InactiveUserAreaGroupBox.Controls.Add($InactiveExtendButton)
#end of groupbox, everything outside of there now
#Close Button
$InactiveClose = New-Object system.windows.Forms.Button
$InactiveClose.Text = "Close"
$InactiveClose.Width = 150
$InactiveClose.Height = 40
$InactiveClose.location = new-object system.drawing.point(710,600)
$InactiveClose.Add_Click({$ViewAllInactiveUsersForm.close()})
$ViewAllInactiveUsersForm.controls.Add($InactiveClose)
#Activate Form
$ViewAllInactiveUsersForm.Add_Shown({$ViewAllInactiveUsersForm.activate()})
[void] $ViewAllInactiveUsersForm.ShowDialog()
}
ViewAllInactiveUsers
If you closely to the layout you will see more differences between the controls (such as shadows).
I believe your issue is caused by the fact that under the ISE Visual Styles are enabled by default. See: Windows Forms look different in PowerShell and Powershell ISE. Why?
So, I guess the solution is to set [Windows.Forms.Application]::EnableVisualStyles() in your cmdlet.
I have a report that is copied to a number of different servers. It is imported manually and the data source properties are altered to match the current server's specs. I would like to be able to automate the process by enabling users to open a the SSRS report and dynamically alter it's shared data source properties through PowerShell. I hope you could help. You may see reference below.
The script would accept an input parameter for servername, username and password. Also, the save my password must be ticked.
I couldn't believe I managed to create a script for this. You may make use of the script below as future reference. Comments are available for each part and anything that needs to be altered has a "here" keyword , ex. Your_database_name_here .
Import-Module SqlPs
#Input parameter to get Server\Instancename of your Datasource
$Servername = Read-Host "Please enter your Servername"
$Instancename = Read-Host "Please enter your Instancename. For default instance please press enter"
Write-host ""
if ($Instancename -eq ""){
$ServerInstance = $Servername
}
Else {
$ServerInstance = $Servername +"\"+ $InstanceName
}
#Setting up SSRS Target URL. This is the location where your reports would be deployed.
if ($Instancename -eq ""){
$ReportServerUri = "http://$Servername/ReportServer//ReportService2010.asmx?wsdl"
$TargetURL = "http://$Servername/Reports"
}
Else {
$ReportServerUri = "http://$Servername/ReportServer_$Instancename//ReportService2010.asmx?wsdl"
$TargetURL = "http://$Servername/Reports_$Instancename"
}
$global:proxy = New-WebServiceProxy -Uri $ReportServerUri -UseDefaultCreden
#We would make use of SQL Server Authentication for the reports shared datasource so you need to supply a username and password.
Write-Host " SQL Server Authentication:"
$Username = Read-Host " Username"
$Password = Read-Host -AsSecureString "Password"
$type = $Proxy.GetType().Namespace
$datatype = ($type + '.Property')
$property =New-Object ($datatype);
$property.Name = “NewFolder”
$property.Value = “NewFolder”
$numproperties = 1
$properties = New-Object ($datatype + '[]')$numproperties
$properties[0] = $property;
$newFolder = $proxy.CreateFolder("Reports”, “/”, $properties);
$newFolder = $proxy.CreateFolder("Data Sources”, “/”, $properties);
$Children =$proxy.ListChildren("/",$false)
$DBname = 'Your_Database_Name_Here'
# Creating Datasource through powershell
Write-Host " Creating Datasource ..."
$Name = "Name_Your_Datasource_here"
$Parent = "/Data Sources"
$ConnectString = "data source=$Servername\$Instancename;initial catalog=$DBname"
$type = $Proxy.GetType().Namespace
$DSDdatatype = ($type + '.DataSourceDefinition')
$DSD = new-object ($DSDdatatype)
if($DSD -eq $null){
Write-Error Failed to create data source definition object
}
$CredentialDataType = ($type + '.CredentialRetrievalEnum')
$Cred = new-object ($CredentialDataType)
$CredEnum = ($CredentialDataType).Integrated
$Cred.value__=1
$DSD.CredentialRetrieval =$Cred
$DSD.ConnectString = $ConnectString
$DSD.Enabled = $true
$DSD.EnabledSpecified = $false
$DSD.Extension = "SQL"
$DSD.ImpersonateUserSpecified = $false
$DSD.Prompt = $null
$DSD.WindowsCredentials = $false
$DSD.UserName = $Username
$DSD.Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
$newDSD = $proxy.CreateDataSource($Name,$Parent,$true,$DSD,$null)
#Deploying RLD files to Target URL
Write-Host " Deploying RDL files ..."
$stream = Get-Content 'D:\Your_RDL_path_here.rdl' -Encoding byte
$warnings =#();
$proxy.CreateCatalogItem("Report","Report_Name_here","/Reports",$true,$stream,$null,[ref]$warnings)
#Let's make use of the datasource we just created for your RDL files.
$Items = $global:proxy.listchildren("/Data Sources", $true)
foreach ($item in $items)
{
$DatasourceName = $item.Name
$DatasourcePath = $item.Path
}
$RDLS = $global:proxy.listchildren("/Reports", $true)
foreach ($rdl in $rdls)
{
$report = $rdl.path
$rep = $global:proxy.GetItemDataSources($report)
$rep | ForEach-Object {
$proxyNamespace = $_.GetType().Namespace
$constDatasource = New-Object ("$proxyNamespace.DataSource")
$constDatasource.Name = $DataSourceName
$constDatasource.Item = New-Object ("$proxyNamespace.DataSourceReference")
$constDatasource.Item.Reference = $DataSourcePath
$_.item = $constDatasource.Item
$global:proxy.SetItemDataSources($report, $_)
Write-Host "Changing datasource `"$($_.Name)`" to $($_.Item.Reference)"
}
}
#Open a IE browser to view the report.
$IE=new-object -com internetexplorer.application
$IE.navigate2($TargetURL)
$IE.visible=$true
Write-Host ""
Write-Host "You may now view the Reports through the open IE browser."
Write-Host -ForegroundColor Green "**STEP COMPLETED!"