Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

17/11/2024

Installing PowerShell on Windows

PowerShell is powerful command line interface for windows PCs and servers, it does get installed by default when you install windows on a PC or server. However, if you want the most up to date version of PowerShell, it is up to you to install it manually. 

Following article is based on Windows 10 PC so some version numbers might be different on different version of Windows.

The PowerShell version you get when you install Windows is called "Desktop Edition". You can see this by running $PSVersionTable command on PowerShell:

As you can see version of this edition of PowerShell is 5.1.xxx.

But there is another Edition written on .Net Core and it is currently on 7.4.xx.


This version is much more sophisticated and have ability to integrate with more modern development environments. There fore if you are a software developer or person who manage modern tools/software, I recommend using this edition of PowerShell.

So let's see how we can install this edition.

Microsoft has comprehensive article on how to install PowerShell in here. Following is simplified version of it.

There are few ways to install PowerShell.

  1. Using Winget
  2. Using MSI package
  3. Using .NET Global Tool
  4. Using Microsoft Store

Using Winget

If you not already aware, winget is the Windows Package Manager. You can run following command on command line (or even default edition of PowerShell) to install via winget:

winget install --id Microsoft.PowerShell --source winget


Using MSI package

This is more traditional way of installing. You will have to manually download it and install it.

Following is the link to the current version of the MSI package -> MSI

This will download the MSI package from GitHub and you will need to save it and run it (note above is x64 version, if you need different version please search for it).

MSI package does give more control over the installation. You can specify various options while installing it. For more information please refer to the original Microsoft article mentioned above.


Using .NET Global Tool

If you have .NET Core SDK already installed on your computer, you can install PowerShell as .NET Global Tool (can invoke from any directory).

Just run following command on command line:

dotnet tool install --global PowerShell


Using Microsoft Store

Like any other application on Microsoft store, PowerShell can be installed from Microsoft Store.









10/06/2024

Creating Windows Task to Call URL

There are many ways you can call a URL from a windows schedule task. Old time we used vbscript to call a URL. We can create a small console app on .Net (Visual Studio) to call a URL and then schedule the console app to run periodically.

But what is the best and most easiest way these days. I wanted to find out recently as I wanted to call a URL from a website periodically to keep the website live (not going to sleep).

Answer I got from several GPTs were to use Powershell scritp.

This is the powershell command to use:

-ExecutionPolicy unrestricted 

    -Command "(New-Object Net.WebClient).DownloadString('YOUR-URL')"


To call this using a Windows Task scheduler do the following steps:

1. Launch Task Scheduler and create a task (don't select basic task)

2. Give an appropriate name and fill out the properties as suited for you

3. Create a trigger to run the task. You can schedule it to run periodically

4. In the "Actions" tab, click new and type "powershell" on "Program/script" box

5. In the "Arguments" box, type the above text (replace the url with your URL).

6. Click ok and save the task.


Make sure to run the script with a user who have appropriate permission to run powershell and commands.




11/11/2023

Downloading RDL files from SSRS Report Server

In work, I got a task to backup SSRS reports folder of a report server. Taking a different approach, I choose to depend on AI tools to help me out. I choose Bing Chat powered by ChatGPT.

Following is the PowerShell code generated by Bing Chat.

# Specify the URL of the report server

$reportServerUrl = "http://myreportserver01//reportserver"


# Specify the path of the report on the report server

$reportPath = "/My Report Folder 01"


# Specify the path where to save the RDL file (Note: double forward slashes)

$savePath = "D:\\BackupFolder\\Transfer\\ReportRDL\\"


# Create a new proxy to the report server (Note: we are using ReportingService2010 name space

$proxy = New-WebServiceProxy -Uri "$reportServerUrl/ReportService2010.asmx?wsdl" -Namespace SSRS.ReportingService2010 -UseDefaultCredential


# Get all reports in the folder

$reports = $proxy.ListChildren($reportPath, $false) | Where-Object { $_.TypeName -eq "Report" }


# Download each report

foreach ($report in $reports)

{

    # Get the report definition

    $reportDefinition = $proxy.GetItemDefinition($report.Path)


    # Save the report definition to disk

    [System.IO.File]::WriteAllBytes($savePath + $report.Name + ".rdl", $reportDefinition)

}


I also come across another away to download RDL files using "RS.exe" tool. It can be found in following url -> https://asgb1.freshdesk.com/support/solutions/articles/11000114924-download-ssrs-reports-rdl-files-using-rs-exe-utility


23/10/2023

Migrating SSRS Report Subscriptions

 SSRS has nice little feature where you can automate emailing of an report on a pre-defined schedule. Last week, one of our customer was moving their SQL server to brand new one and wanted to move all SQL related feature to it. 

So I have moved everything and was left with few dozens of report subscriptions. I wondering whether I should re-create them manually on new server or is there a tool that I can use.

When asked from AI search tool, it came up with several tools. Out of them following two were my finalists.

1. Report Sync Tool -> https://code.google.com/archive/p/reportsync/downloads

2. ReportingServicesTools written in PowerShell

I decided to go with PowerShell, because of my love with PowerShell.

ReportingServicesTools is a PowerShell cmdlet is a tool that we can use to do various tasks related to SSRS. It is hosted on GitHub -> ReportingServices

Documentation contains how to install it and basic use of it. However, I couldn't find enough documentation regarding how to do a report subscription migration.

There were some information in here, but nothing much.

So I decided to dig little deep into the script and see what is it capable of. I was working with version 0.0.8.0.

There are about 70 odd functions in that cmdlet, but I was interested on functions relate to subscriptions.

  • Get-RsSubscription
  • Set-RsSubscription
  • Copy-RsSubscription
  • Export-RsSubscriptionXml
  • Remove-RsSubscription
  • New-RsSubscription
  • Import-RsSubscriptionXml

In order to fetch subscriptions under a reporting folder I used following:

Get-RsSubscription -ReportServerUri 'http://remote-machine:8080/reportserver_sql16' -RsItem '/path/to/my/report'

In -RsItem parameter you can specify a folder or a report it self. If you specify a folder, it will look for all subscriptions under that folder. If you specify a report, it will look for all subscription belong to that report only.

Above assume current windows user credentials when connecting to report server. But if you want to connect to report server using different credentials use the usual methods to specify credentials for -Credential parameter.

$password = ConvertTo-SecureString "MyPlainTextPassword" -AsPlainText -Force

$Cred = New-Object System.Management.Automation.PSCredential ("username", $password)


Now I got subscriptions, I want to write them to a file, so I can transfer them to the new server (if the new server is accessible from where you are you can directly use the Copy-RsSubscription function).

To export to a file, I have used Export-RsSubscriptionXml function.

Get-RsSubscription <<parameters as per above>> | Export-RsSubscriptionXml C:\Test\MySubscriptions.xml

You can pipe the output of the Get-RsSubscription function to Export function.


Then on the new server, I have copied above xml file to a directory and used Import-RsSubscription to import them to new Reporting server.

Import-RsSubscriptionXml C:\Test\\MySubscriptions.xml -ReportServerUri 'http://remote-machine:8080/reportserver_sql16'

This will just create Powershell objects from the xml file, you need to output this to a Copy-RsSubscription or Set-RsSubscription function.

Different between Copy and Set functions are, Copy create new subscriptions, where set updates already existing subscription.

If you want just to see, what is in a xml file you can use command like below:

            Import-RsSubscriptionXml .\MySubscriptions.xml | 

            Out-GridView -PassThru |

            Copy-RsSubscription -RsItem /Example/Report


You can learn more about these functions, from source code help available on the GitHub -> CatalogItems.


Using Own API keys in Various IDEs

AI hype is so high these days, every one want best AI models for least cost. Though they don't cost much individually, when you add up c...