Powershell Like operator invokes REST error - powershell

I am working on a custom PoweShell module picked up from the internet for our BI application.
My question is rather simple, the code below does not work:
Get-QlikDataConnection -filter "name -like 'Data*'"
And throws an error like:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At C:\Program Files\WindowsPowerShell\Modules\Qlik-Cli\1.13\functions\core.ps1:32 char:15
+ ... $result = Invoke-RestMethod -Method $method -Uri $path #params -Web ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
However the code below works fine and shows me the correct output:
Get-QlikDataConnection -filter "name eq 'DataPrepAppCache'"
Am I doing something wrong or do some modules not understand a few operators?

After having a look at the source of the module you're using, Qlik-Admin-Utils, I would not use the -filter param, as the input you specify there gets processed by this block within the Invoke-QlikGet cmdlet:
If( $filter ) {
If( $path.contains("?") ) {
$path += "&filter=$filter"
} else {
$path += "?filter=$filter"
}
}
This script appends your filter as a query parameter in the URL, and it doesn't support regular PowerShell formatting, but sends the filter over to qLik's REST API.
If I were writing this, I'd ignore their filtering and do the following:
$Connections = Get-QlikDataConnection
$DataConnection = $Connections | Where name -like "Data*"
This is more likely to just work with less fiddling.
However, if you want to use Qlik's Filter support, I found this so you can read up on the syntax of it here.
It looks like they do offer a filter of their own which might help, it's the Starts With filter, defined as SW, for a syntax of Name sw 'Data'. You might try this and see if it works instead.
Get-QlikDataConnection -filter "name sw 'Data'"

Related

Why is Export-PnPFlows Skipping Flows?

This is my first post, so please pardon formatting errors!
I've been trying to export my tenant's Power Automate flows via Export-PnPFlow. I have a few hundred flows, so doing it by hand isn't really feasible.
The script works well enough for some flows, but is throwing an error for others, but I can't see why.
It does not seem to be caused by if it's enabled/disabled, owned by a certain user, in a certain environment, or in/out of a solution.
The ones that work, work perfectly; the others give the following error:
Export-PnPFlow : {"error":{"code":"ConnectionAuthorizationFailed","message":"The caller object id is '08#####-#####-####-###'. Connection '2#####-#####-####-####' to 'shared_logicflows' cannot be used to activate this flow, either because
this is not a valid connection or because it is not a connection you have access permission for. Either replace the connection with a valid connection you can access or have the connection owner activate the flow, so the connection is shared with you in the context of
this flow."}}
At C:\Users\jutrust\script.ps1:13 char:21
+ ... Export-PnPFlow -Environment $environment -Identity $flow. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Export-PnPFlow], HttpRequestException
+ FullyQualifiedErrorId : System.Net.Http.HttpRequestException,PnP.PowerShell.Commands.PowerPlatform.PowerAutomate.ExportFlow
My question is, is it possible that these flows are deleted and that's why I get this error? If so, how can I check?
Code below.
Connect-PnPOnline -url https://########.sharepoint.com
$environments = get-pnppowerplatformenvironment
foreach($environment in $environments)
{
$flows = Get-PnPFlow -Environment $environment -AsAdmin
foreach ($flow in $flows)
{
$filename = $flow.Properties.DisplayName.Replace(" ", "")
$timestamp = Get-Date -Format "yyyymmddhhmmss"
$exportPath = "$($filename)_$($timestamp)"
$exportPath = $exportPath.Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
Export-PnPFlow -Environment $environment -Identity $flow.Name | Out-File "C:\Users\jutrust\documents\$exportPath.json"
}
}
Help!
Edit: Updated error code

Update AddresBookPolicy AddressLists based on variable input

I am trying to update an Address Book Policy on Exchange Online.
Idea is that I parse some Address Lists and save these into a variable.
These could be passed into the Set-AddresBookPolicy.
So I start off with parsing these adresses:
$AddressLists = (Get-AddressList).Id | ? {$_ -like "*Company_1*"}
This results an array like \Company_1_Users, \Company_1_Contacts, \Company_1_DLs as expected.
I apply these with
Set-AddressBookPolicy -Identity "Company1" -AddressLists $AddressLists `
-RoomList "C1_Rooms" -GlobalAddressList "C1_GAL" -OfflineAddressBook "C1_OAB"
Result is an error:
WARNING: An unexpected error has occurred and a Watson dump is being generated: The operation can't be performed on this object because its status isn't valid.
The operation can't be performed on this object because its status isn't valid.
+ CategoryInfo : NotSpecified: (:) [Set-AddressBookPolicy], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Exchange.Management.SystemConfigurationTasks.SetAddressBookPolicy
+ PSComputerName : outlook.office365.com
I've tried converting it to a string (with -join ',') and have tried casting it, but I can't get further then an error (which then is of another kind).
If I copy the output and then type it into the command, it works. So that part is correct. However, I would like to automate this.
Does anyone know how I can correctly provide an input into the below cmdlet and have it running as expected?
EDIT: added full script below:
$AddressLists = #()
$AddressLists = (Get-AddressList).Id | ? {$_ -like "*Company_1*"}
$AddressLists = $AddressLists -join ',' #Adding this line just results in another error...
Set-AddressBookPolicy -Identity "Company1" -AddressLists $AddressLists `
-RoomList "C1_Rooms" -GlobalAddressList "C1_GAL" -OfflineAddressBook "C1_OAB"
The result of $AddressLists is an array (System.Array) with contents:
\Company_1
\Company_1Country1
\Company_1Country2
\Company_1Department1
\Company_1Department2
If your variable produces what you are saying...
$AddressLists = (Get-AddressList).Id | {$_ -like "*Company_1*"}
\Company_1_Users,
\Company_1_Contacts,
\Company_1_DLs
Then In Theory When You Add It Into a ForEach Loop It Should Work Accordingly. I Don't Have Exchange To Test It (by removing $updatecommand and leaving the execution command :o)
Change the settings of an address book policy in Exchange Online
<https://learn.microsoft.com/en-us/exchange/address-books/address-book-policies/change-the-settings-of-an-address-book-policy>
$AddressLists = ("\Company_1_Users", "\Company_1_Contacts", "\Company_1_DLs")
$iD = "Company1"
$rL = "C1_Rooms"
$gAL = "C1_GAL"
$oAB = "C1_OAB"
ForEach($AddressList in $AddressLists){
Write-Host "Without an Exchange Server, I'm Just Demonstating The Update Process"
Write-Host "The AddressList Being Updated Is -- $AddressList"
$updatecommand = "Set-AddressBookPolicy -Identity $iD -AddressLists $AddressList -RoomList $rL -GlobalAddressList $gAL -OfflineAddressBook $oAB"
Write-Host $updatecommand
}

Add elements to arry in powershell

I want to simply add some numbers to an array and then sort them via powershell, however, the following code seems to be wrong
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
$myArray.Add($Tokens[$Tokens.Count-1])
}
Write-Host($myArray | Sort-Object)
The error is
+ $myArray.Add($Tokens[$Tokens.Count-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
How can I fix that?
The variable $Name is something like 101.u18.uab.14 or 103.win10.template or 102.win7.pink.18 and so on. Each $Name has some . symbols and I want to tokenize them and get the last element for each of them. So, in this example, I want to see a sorted 14 18 template.
UPDATE:
The provided methods seems to be incorrect.
1- This method by Steven
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
[Void]$myArray.Add($Tokens[-1])
}
shows this error
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At C:\Users\user\Desktop\get_ip_list.ps1:20 char:5
+ [Void]$myArray.Add($Tokens[-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
2- This method by Santiago
$myArray = [Collections.Generic.List[string]]::new()
Foreach ($Name in $VMName) {
[Void]$myArray.Add($Name.Split(".")[-1])
}
Shows the following error
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At C:\Users\user\Desktop\get_ip_list.ps1:19 char:5
+ [Void]$myArray.Add($Name.Split(".")[-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
If I have missed your point in the above codes, please let me know.
I think you are missing the first line from the error. However it looks like you are simply trying to add the last elements from the $Tokens array. In that case you don't need to reference the index like that, below should work:
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
[Void]$myArray.Add($Tokens[-1])
}
Notice the addition of [Void] this will stop the .Add() method from returning the index number it just added to.
Also note you can create array list objects using casting like:
$myArray = [Collections.ArrayList]#()
Update to Address Continued Errors:
The only thing I can think of to cause the error "Collection was of a fixed size." is if you've previously type constrained the variable.
Example:
[String[]]$myArray = #()
# Posibly a whole bunch of other things happening maybe in the console or IDE.
$myArray = [Collections.ArrayList]#()
$myArray.Add('something')
Results:
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $myArray.Add('something')...
In this case they type of the $myArray will not change to [Collections.ArrayList]. The problem will be transparent up until you try to use the .Add() method that won't work. This is because an array list is easily and therefore silently cast back to a [String[]] or [Object[]].
Note: If you were to run $myArray.IsFixedSize it would return "True".
My guess as to what's happening; at some point while developing your code or perhaps in the larger script, $myArray got type constrained, and stuck in the scope. This can definitely happen especially given the scope overlap in IDE's like PowerShell's ISE, and I think it happens in VSCode as well. If this is part of a larger script look for instances of $myArray to see if it's indeed type constrained and make corrections as needed. Otherwise a simply restarting your session might do the trick.
Honestly, not sure how could you be getting that error unless the array we're looping through is actually something different. Steven's answer should work fine, I'll put this code below just to show that the results we get are the ones you expect:
$col = [Collections.Generic.List[String]]::new()
$vmName = #(
'101.u18.uab.14'
'103.win10.template'
'102.win7.pink.18'
)
ForEach($name in $vmName)
{
$col.Add($name.Split('.')[-1])
}
if you want absolutly use array you can simply do it :
$Array=#()
$VMName | %{
$Value=($_.Split('.'))[-1]
$Array+= $Value
}
$Array| sort
Otherwise you can simply do it :
$VMName | %{($_.Split('.'))[-1]} | sort

Powershell v2.0 Foreach loop failing when run on PS v4

Hi is there a difference in the way that PS v4 (running from a 2012 server) reacts to a Foreach loop written in V2?
I have this simple script that works in v2 but produces an error on v4:
$Serverlist = Import-CSV "D:\PendingReboot\AllADServers.csv"
Foreach ($Computer.Name in $Serverlist)
{
$ADComputer = $Computer.Name
$ADOwner = $Computer.Description
If (!($Computer.Description))
{$ADOwner = "Unassigned"}
$ADOU = $AD | select DistinguishedName
"$ADComputer $ADOwner"
}
Server1 Team1
Server2 Team2
etc. etc.
The Error in V4 is:
At D:\PendingReboot\Test.ps1:2 char:23
+ Foreach ($Computer.Name in $Serverlist)
+ ~
Missing 'in' after variable in foreach loop.
At D:\PendingReboot\Test.ps1:2 char:43
+ Foreach ($Computer.Name in $Serverlist)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingInInForeach
PetSerAl's comment is correct. You should be using foreach ($Computer in $ServerList). As for why it works in PowerShell v2 and not in v4, well I can only assume that they corrected the behavior, as it should have failed in v2.
Basically your .Name in that portion is doing nothing, as evidenced by the fact that you still needed to specify .Name on the very first line within the loop.
Essentially, this was a syntax error that wasn't caught in an earlier version.

Delete a wiki page with powershell

I need help with my code.
I first tried to create a new wiki page in SharePoint 2013 and that worked perfectly.
Now I'm trying to delete a wiki page and that doesn't work very well.
My code:
Add-Type –Path "C:\Users\Benutzername\AppData\Local\Apps\OfficeDevPnP.PowerShell.V15.Commands\Modules\OfficeDevPnP.PowerShell.V15.Commands\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Users\Benutzername\AppData\Local\Apps\OfficeDevPnP.PowerShell.V15.Commands\Modules\OfficeDevPnP.PowerShell.V15.Commands\Microsoft.SharePoint.Client.Runtime.dll"
Function Delete-WikiPage ([Microsoft.SharePoint.Client.ClientContext]$Context, [string]$WikiLibraryTitle,[string]$PageName)
{
$wikiLibrary = $context.Web.Lists.GetByTitle($wikiLibraryTitle)
$context.Load($wikiLibrary.RootFolder.Files)
$context.ExecuteQuery()
$wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
$context.Load($wikiPage)
$context.DeleteObject()
}
$Url = "hhtps://sharepoint.com"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$pageName = "Testlauf.aspx"
Delete-WikiPage -Context $context -WikiLibraryTitle "Testwiki" -PageName $pageName
I got an error message about the DeleteObject() method but I didn't find anything how I can fix that:
Error when calling the method [Microsoft.SharePoint.Client.ClientContext] no method found with the name "DeleteObject".
In C:\Users\Benutzername\Desktop\Projektarbeit_Wiki\PowerShell\Delete_WikiPage.ps1:12 Zeichen:5
+ $context.DeleteObject()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
I can only work with methods from the Add-Types. Can anyone help me with this?
You use the CSOM in PowerShell. (you load the CSOM DLL ).
So you should search information with SharPoint CSOM.
To answer you question, you use : DeleteObject() on you object : context. This methode doesn't exist on the Context Object. But the methode exist on the File.
Si you should try something like :
$wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
$context.Load($wikiPage)
$context.ExecuteQuery() //Get the item, but not sure this is needed
$wikiPage.DeleteObject()
$context.ExecuteQuery()
personal Note :
You should use CAML query to retrieve your WikiPage, not this: $wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
With this kind of query you have bad performance, and you can raise exception list threshold