cannot import google calendar api library to PowerShell - powershell

I am trying to authenticate to google calendar using PowerShell. To do that, I have to install the "Google.Apis.Calendar.v3". And to do that I have to install the nuget package source. So I start with nuget.
PS C:\Windows\system32> register-PackageSource -Name MyNuGet -Location https://www.nuget.org/api/v2 -ProviderName NuGet
Name ProviderName IsTrusted Location
---- ------------ --------- --------
MyNuGet NuGet False https://www.nuget.org/api/v2
Then I install "Google.Apis.Calendar.v3" and get an error that there's a dependency loop.
PS C:\Windows\system32> Install-Package Google.Apis.Calendar.v3
Install-Package : Dependency loop detected for package 'Google.Apis.Calendar.v3'.
At line:1 char:1
+ Install-Package Google.Apis.Calendar.v3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : Deadlock detected: (Google.Apis.Calendar.v3:String) [Install-Package], Exception
+ FullyQualifiedErrorId : DependencyLoopDetected,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
So I add the -SkipDependencies option and the package gets installed.
PS C:\Windows\system32> Install-Package Google.Apis.Calendar.v3 -SkipDependencies
Name Version Source Summary
---- ------- ------ -------
Google.Apis.Calendar.v3 1.59.0.2759 MyNuGet Google APIs Client Library for working with Calendar v3....
I then try to authenticate with google calendar but get a message that the assembly is not loaded.
PS C:\Windows\system32> $credential = New-Object Google.Apis.Auth.OAuth2.ServiceAccountCredential
(
[Google.Apis.Auth.OAuth2.ServiceAccountCredential]::FromJsonFile("C:\Users\Windows\Desktop\calendar-346619-92e3766da662.json")
)
New-Object : Cannot find type [Google.Apis.Auth.OAuth2.ServiceAccountCredential]: verify that the assembly containing this
type is loaded.
At line:1 char:15
+ ... redential = New-Object Google.Apis.Auth.OAuth2.ServiceAccountCredenti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
Unable to find type [Google.Apis.Auth.OAuth2.ServiceAccountCredential].
At line:3 char:5
+ [Google.Apis.Auth.OAuth2.ServiceAccountCredential]::FromJsonFile( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Google.Apis.Aut...countCredential:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
So I search for "google.apis.calendar.v3.dll" on my computer, find the .dll and try to load it but I get an error again.
PS C:\Windows\system32> Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Google.Apis.Calendar.v3.1.59.0.2759\lib\net45\Google.Apis.Calendar.v3.dll"
Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
At line:1 char:1
+ Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Goo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
+ FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
I'm using Windows 10 and PowerShell version 5.1.19041.2364
How can I import this API library to PowerShell so I can authenticate to google calendar?

For starters, I got really angry that it was so difficult to install the packages, so this ought to fix that problem:
#requires -Modules #{ModuleName = 'PackageManagement'; ModuleVersion = '1.4.8.1'}
function Install-PackageRecursive
{
<#
.EXAMPLE
Install-PackageRecursive Google.Apis.Calendar.v3
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory, ValueFromPipeline)]
[string]$Name,
[ValidateSet('CurrentUser', 'AllUsers')]
[string]$Scope = 'CurrentUser',
[SupportsWildcards()]
[string[]]$Exclude = ('NETStandard.Library', 'Microsoft.*', 'System.*'),
[switch]$Force,
[Parameter(DontShow)]
$Packages
)
begin
{
if (-not $Packages)
{
$Packages = #{}
$InstalledPackages = Get-Package
$InstalledPackages | ForEach-Object {
$Key = $_.CanonicalId -replace '#.*'
$Packages[$Key] = $_
}
$PSBoundParameters.Packages = $Packages
}
}
process
{
[void]$PSBoundParameters.Remove('Name')
$Name, $Version = $Name -replace '^nuget:' -split '/'
$Params = #{Name = $Name}
if ($Version)
{
$Params.RequiredVersion = $Version
}
if ($Exclude | Where-Object {$Name -like $_})
{
Write-Verbose "Excluded: $Name"
return
}
$Key = "nuget:$Name/$($Params.Version)"
$Package = $Packages[$Key]
if (-not $Package)
{
$Package = Get-Package #Params -ErrorAction Ignore
}
if (-not $Package)
{
$Package = Find-Package #Params
}
$Key = $Package.CanonicalId -replace '#.*' # in case it's different
$Packages[$Key] = $Package
# e.g. nuget:Google.Apis/1.59.0
if ($Package.Dependencies)
{
Write-Verbose "Installing dependencies for: $Key" -Verbose
$Package.Dependencies |
Sort-Object -Unique |
Install-PackageRecursive #PSBoundParameters
}
if ($Package.Status -eq 'Installed')
{
Write-Verbose "Already installed: $Key" -Verbose
return
}
Write-Verbose "Installing: $Key" -Verbose
$null = Install-Package #Params -Scope $Scope -SkipDependencies -Force:$Force
if ($?)
{
$Package = Get-Package #Params
Write-Verbose "Installed $Key to '$(Split-Path $Package.Source)'." -Verbose
$Packages[$Key] = $Package
$Package
}
}
}
After getting that done, I found that all packages have to be imported before you can use any:
[ValidateSet('net45', 'netstandard1.3', 'netstandard2.0')]$Framework = 'net45'
$ImportOrder = (
'Newtonsoft.Json',
'Google.Apis.Core',
'Google.Apis',
'Google.Apis.Auth',
'Google.Apis.Calendar.v3'
)
$Packages = $ImportOrder | ForEach-Object {Get-Package $_}
$Packages | ForEach-Object {
$Folder = $_.Source |
Split-Path |
Join-Path -ChildPath lib |
Join-Path -ChildPath $Framework
$Path = Join-Path $Folder "$($_.Name).dll"
Add-Type -Path $Path
}
Then it's showtime:
> [Google.Apis.Calendar.v3.CalendarService]::new
OverloadDefinitions
-------------------
Google.Apis.Calendar.v3.CalendarService new()
Google.Apis.Calendar.v3.CalendarService new(Google.Apis.Services.BaseClientService+Initializer initializer)

Related

Getting Registry Key of all machines in the domain

sorry if it's a silly question. I'm trying to get the "EnableDCOM" Registry Key of all the machines on the domain and disable them. I'm kinda stuck with getting the status of the registry key.
Get-Adcomputer -Filter * | Get-itemProperty -path HKLM:\Software\Microsoft\OLE -name "EnableDCOM"
Here is the error:
Get-ItemProperty : Cannot process argument transformation on parameter 'Credential'. userName
At line:1 char:28
... -filter * | Get-ItemProperty -path HKLM:\Software\Microsoft\OLE -name ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidData: (CN=DAYGROUP-PCI...=daygroup,DC=ca:PSObject) [Get-ItemProperty], ParameterB
indingArgumentTransformationException
FullyQualifiedErrorId : ParameterArgumentTransformationError,Microsoft.PowerShell.Commands.GetItemPropertyComman
d
After trying to use the domain admin credential:
The provider does not support the use of credentials. Perform the operation again without specifying credentials.
At line:1 char:1
get-adcomputer -filter * | Get-ItemProperty -path HKLM:\Software\Micr ...
+ CategoryInfo : NotImplemented: (:) [], PSNotSupportedException
+ FullyQualifiedErrorId : NotSupported
This code can help you. Don`t forget to specify different credential if needed.
$ADComputers = ( Get-ADComputer -Filter * ).Name
$ResultArray = #()
foreach ( $Computer in $ADComputers ){
#Maybe you need specify different credential -Credential $cred
$Value = Invoke-Command -ComputerName $Computer -ScriptBlock {
$Value = Get-ItemPropertyValue -path 'HKLM:\Software\Microsoft\OLE' -name 'EnableDCOM'
return $Value
}
$PSO = [PSCustomObject]#{
Computer = $Computer
Value = $Value
}
$ResultArray += $PSO
}
$ResultArray

Issue while executing get-acl for remote servers

I am having the below code to get the data from remote servers. thanks to #Santiago Squarzon
$serverlist = Get-Content -Path "C:\ServerList.txt"
# Collect results here
$result = Invoke-Command -ComputerName $serverlist -ScriptBlock {
$paths_list = $env:Path -Split [System.IO.Path]::PathSeparator
foreach($sys_Path in $paths_list)
{
$Permissions = (Get-Acl -Path $sys_Path).Access
foreach($acl in $Permissions)
{
if(-not $acl.IdentityReference)
{
continue
}
[pscustomobject]#{
ComputerName = $env:ComputerName
SystemFolderPath = $sys_Path
IdenityReference = $acl.IdentityReference.Value
FileSystemRights = $acl.FileSystemRights
}
}
}
} -HideComputerName
$result | Export-Csv -Path "C:\status_report.csv" -NoTypeInformation
But I am getting below error while executing it
Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again.
+ CategoryInfo : InvalidData: (:) [Get-Acl], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAclCommand
+ PSComputerName
Please let me know on this.
Might adding the following check before $Permissions = (Get-Acl -Path $sys_Path).Access would resolve the issue:
if (($sys_Path -eq $null) -or ($sys_Path -eq '') ) {
continue
}

I am trying to automate the config of TCPPort number 1433 on SQL Server 2016 and 2019 with a PS script

Inspired by this approach:
$pcName = $env:COMPUTERNAME
($dbmsName = Invoke-Sqlcmd -Query "SELECT ##servicename")
# Loading SQLPS environment
Import-Module SQLPS -DisableNameChecking -Force
# Initializing WMI object and Connect to the instance using SMO
($Wmi = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $pcName)
($uri = "ManagedComputer[#Name='$pcName']/
ServerInstance[#Name='$dbmsName']/ServerProtocol[#Name='Tcp']")
# Getting settings
($Tcp = $wmi.GetSmoObject($uri))
$Tcp.IsEnabled = $true
($Wmi.ClientProtocols)
# Setting IP properties
$wmi.GetSmoObject($uri + "/IPAddress[#Name='IPAll']").IPAddressProperties[1].Value="1433"
# Save properties
$Tcp.Alter()
# Restart service
Restart-Service -Name MSSQL* -Force
Start-Sleep -s 30
Whenever I try to test with lastest version of PowerShell(5.1.17763.2090) on both SQL Servers 2016 and 2019: I get an error:
PS C:\Windows\system32> ($Tcp = $wmi.GetSmoObject($uri))
Exception calling "GetSmoObject" with "1" argument(s): "Attempt to retrieve data for object failed for ManagedComputer
'DB2T-30223'."
At line:1 char:2
+ ($Tcp = $wmi.GetSmoObject($uri))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FailedOperationException
What am I missing?
I found this on stack - Use PowerShell To Enable TCP/IP in SQL Server Configuration Manager
It seems like the same error place '+ ($Tcp = $wmi.GetSmoObject($uri))'
$pcName = $env:COMPUTERNAME
$wmi = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $pcName
$wmiinstance = $wmi.ServerInstances | Where-Object { $_.Name -eq $wmiinstancename }
$tcp = $wmiinstance.ServerProtocols | Where-Object { $_.DisplayName -eq 'TCP/IP' }
$IpAddress = $tcp.IpAddresses | where-object { $_.IpAddress -eq $IpAddress }
$tcpport = $IpAddress.IpAddressProperties | Where-Object { $_.Name -eq 'TcpPort' }

Powershell, Get-WindowsCapability: Why am I getting an error about the path format being unsupported?

I am running the script pasted below:
function grep { $input | out-string -stream | select-string $args }
function Install-SSH { Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*' | grep('Name\s*:\s(\S+)') | % { Add-WindowsCapability -Online -Name $_.matches.groups[1].Value } }
function Uninstall-SSH { Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*' | grep('Name\s*:\s(\S+)') | % { Remove-WindowsCapability -Online -Name $_.matches.groups[1].Value } }
if((([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) {
#Administrator Payload goes here.
Write-Host -ForegroundColor Green "Installing SSH..."
Get-WindowsCapability -Online
Pause #Debugging purposes
} else {
Write-Host -ForegroundColor Red "Powershell script running as Guest. Trying to run as administrator..."
Set-Location -Path $PSScriptRoot
$registryPath = "HKCU:\Environment"
$Name = "windir"
$Value = "powershell -ep bypass -w n $PSCommandPath;#"
Set-ItemProperty -Path $registryPath -Name $name -Value $Value
schtasks /run /tn \Microsoft\Windows\DiskCleanup\SilentCleanup /I | Out-Null
Remove-ItemProperty -Path $registryPath -Name $name
}
And I am getting the following error in the output, and I can find nothing online that helps with understanding what is going on here. What am I doing wrong?
Installing SSH...
Get-WindowsCapability : Set current directory to C:\WINDOWS\system32 failed:The given path's format is not supported.
At D:\Code\projects\powershell-scripts\SSHInstallation.ps1:10 char:5
+ Get-WindowsCapability -Online
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Microsoft.Dism.Commands.BaseDismObject:BaseDismObject) [Get-WindowsCapabi
lity], Exception
+ FullyQualifiedErrorId : BaseCmdLet,Microsoft.Dism.Commands.GetWindowsCapabilityCommand

New-Item IIS:\Sites\SiteName - Index was outside the bounds of the array

I created a function to create a site in IIS, but I"m running into a bizzare error.
Here's the url I've been using as a reference:
http://learn.iis.net/page.aspx/433/powershell-snap-in-creating-web-sites-web-applications-virtual-directories-and-application-pools/
New-Item : Index was outside the bounds of the array.
At line:1 char:9
+ New-Item <<<< 'IIS:\Sites\SiteName' -physicalPath "$sitePath" -bindings #{protocol="$protocol";bindingInformation="$fullBindings"}
+ CategoryInfo : NotSpecified: (:) [New-Item], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.Commands.NewItemCommand
Here's the code block that calls the function:
function Create-FullSite($site, $framework, $userName, $password, $protocol, $port, $enabledProtocols)
{
#Write-Host "Prompting for path to "
$sitePath = Select-Folder
#Write-Host $sitePath
#Write-Host "Setting up app pool for "
$csServicePool = New-Item -Path iis:\AppPools\$site
#Write-Host "Configuring app pool"
Set-ItemProperty -Path IIS:\AppPools\$site -name managedRuntimeVersion -value $framework
$csServicePool.processModel.username = $userName
$csServicePool.processModel.password = $password
$csServicePool.processModel.identityType = 3
$csServicePool | set-item
#Write-Host "Creating IIS Site "
$fullBindings = ':'+$port.ToString()+':'
Write-Host $fullBindings
Write-Host $site
Write-Host $sitePath
Write-Host $protocol
New-Item IIS:\Sites\$site -physicalPath "$sitePath" -bindings #{protocol="$protocol";bindingInformation="$fullBindings"}
#Write-Host "Assigning App pool to "
Set-ItemProperty -Path IIS:\Sites\$site -name ApplicationPool -value $site
#Write-Host "setting applicationDefaults.enabledProtocols: "
Set-ItemProperty -Path IIS:\Sites\$site -name applicationDefaults.enabledProtocols -value "$enabledProtocols"
return $sitePath
}
$ServicesSiteName = 'MyNewSite'
$ServicesPort = '80'
$ServiceBindings = 'http'
$csWebServiceUserName = 'domain\someUser'
$csWebServicePassword = 'AReallyComplexPassword'
$v2Framework = 'v2.0'
$v4Framework = 'v4.0'
Create-FullSite $ServicesSiteName $v2Framework $csWebServiceUserName $csWebServicePassword $ServiceBindings $ServicesPort $ServiceBindings
It turns out if you delete all the websites in IIS, the "Index Out of Range" exception is always thrown. I have a feeling it's trying to generate a site id and cannot find the next one in the list. This article helped me solve the issue. http://forums.iis.net/t/1159761.aspx