Monday, March 25, 2013

PowerShellPack TaskScheduler Remove-Task.ps1 Method invocation failed because [System.String] doesn't contain a method named 'DeleteTask'

I am using the Remove-Task cmdlet in the TaskScheduler module of PowerShellPack to manage some Task Scheduler tasks and getting this weird error:
Method invocation failed because [System.String] doesn't contain a method named 'DeleteTask'

So, I took a look at the code found at ...\Documents\WindowsPowerShell\Modules\TaskScheduler\Remove-Task.ps1 and found an error. The version I have looks like this at lines 53-58:

        switch ($psCmdlet.ParameterSetName) {
            Task { 
                $scheduler = Connect-ToTaskScheduler -ComputerName $ComputerName -Credential $Credential
                $folder =$scheduler.GetFolder("")
                $folder.DeleteTask($task.Path, 0)

The problem is that there is a [String[]] $Folder = "" declaration earlier on in the code.

I changed it to the following and it looks like it works:
        switch ($psCmdlet.ParameterSetName) {
            Task { 
                $scheduler = Connect-ToTaskScheduler -ComputerName $ComputerName -Credential $Credential
                $taskfolder =$scheduler.GetFolder("")
                $taskfolder.DeleteTask($task.Path, 0)

Coincidentally, the Get-ScheduledTask.ps1 cmdlet uses the same idea so the original author must have just missed this. The module looks like it hasn't been updated for quite some time.

Thursday, March 21, 2013

Bulk reset of passwords

Recently, I had to reset a whole bunch of passwords. This can be easily done via PowerShell.

$Excludes = @('user1', 'user2')
$Password = ConvertTo-SecureString -AsPlainText -Force 'mypassword'
$SearchBase = "OU=Some OU,DC=Some DC,DC=Some DC"

Import-Module ActiveDirectory

Get-ADUser -Filter * -SearchBase $SearchBase | Where-Object {$Excludes -notcontains $_.SamAccountName} | Set-ADAccountPassword -NewPassword $Password

Then to check what was done:

Get-ADUser -Filter * -Properties SamAccountName, PasswordLastSet | Select-Object SamAccountName, PasswordLastSet | Sort-Object PasswordLastSet -Descending

Extracting data from SQL Azure using Powershell

I have a SQL Website that is logging some debug output to a SQL Azure DB Linked Resource. In order to do some quick troubleshooting, I tried using the Manage interface in Azure, however it is lacking a way to export the data. I could manually copy and paste out database rows and columns, but that is a pain. I don't have SQL 2013 Management Studio and just want a lightweight way for me to access my databases.

I ended up doing this with Powershell:


$connectionString = "Data Source=$datasource;Initial Catalog=$database;User ID=$userid;Password=$password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
$SqlCmd=New-Object System.Data.SqlClient.SqlCommand($CommandText)
$SqlCmd.Connection=New-Object System.Data.SqlClient.SqlConnection($ConnectionString)

$SqlAdapter=New-Object System.Data.SqlClient.SqlDataAdapter($SqlCmd)
$DataSet=New-Object System.Data.DataSet
I can then get at the data through $DataSet.

I can also get the connection string though the Windows Azure console and go to SQL Databases, select my database, select "View SQL Database connection strings for ADO .Net, ODBC, PHP, and JDBC", copy the ADO.NET connection string, and replace "Server" with "Data Source", "Database" with "Initial Catalog", and "{your_password_here}" with my password.

Error: The configuration section 'microsoft.applicationServer' cannot be read because it is missing a section declaration

Recently, I have been trying to install some code on a brand new server running Windows 2008 R2 SP1 and .NET 4.0. However, I kept running into the following error: Error: The configuration section 'microsoft.applicationServer' cannot be read because it is missing a section declaration

There are many resources out there talking about adding various things. It turns out that what I was missing was AppFabric.

Ouch! I wasted a lot of time on that one.