Forum Discussion

InhumaneTarball's avatar
InhumaneTarball
Copper Contributor
Aug 17, 2024

No types for object data?

I'm tryna write a script that downloads winget.

 

$WingetLatestReleaseURL = "https://api.github.com/repos/microsoft/winget-cli/releases/latest"
$WingetLatestReleaseData = $(Invoke-WebRequest -Uri $WingetLatestReleaseURL | ConvertFrom-Json )

for ($i = 0; $i -le $WingetLatestReleaseData.assets.Count; $i++) {
    $AssetData = $WingetLatestReleaseData.assets[$i]
    
    echo $AssetData
}

 

AssetData always has data:

url                  : https://api.github.com/repos/microsoft/winget-cli/releases/assets/178799925
id                   : 178799925
node_id              : RA_kwDOC8It-s4KqEU1
name                 : Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt
label                : 
uploader             : @{login=ryfu-msft; id=69221034; node_id=MDQ6VXNlcjY5MjIxMDM0; 
                       avatar_url=https://avatars.githubusercontent.com/u/69221034?v=4; gravatar_id=; 
                       url=https://api.github.com/users/ryfu-msft; html_url=https://github.com/ryfu-msft; 
                       followers_url=https://api.github.com/users/ryfu-msft/followers; 
                       following_url=https://api.github.com/users/ryfu-msft/following{/other_user}; 
                       gists_url=https://api.github.com/users/ryfu-msft/gists{/gist_id}; 
                       starred_url=https://api.github.com/users/ryfu-msft/starred{/owner}{/repo}; 
                       subscriptions_url=https://api.github.com/users/ryfu-msft/subscriptions; 
                       organizations_url=https://api.github.com/users/ryfu-msft/orgs; 
                       repos_url=https://api.github.com/users/ryfu-msft/repos; 
                       events_url=https://api.github.com/users/ryfu-msft/events{/privacy}; 
                       received_events_url=https://api.github.com/users/ryfu-msft/received_events; type=User; site_admin=False}
content_type         : text/plain
state                : uploaded
size                 : 64
download_count       : 46017
created_at           : 2024-07-10T16:04:38Z
updated_at           : 2024-07-10T16:04:38Z
browser_download_url : https://github.com/microsoft/winget-cli/releases/download/v1.8.1911/Microsoft.DesktopAppInstaller_8wekyb3d8bbw
                       e.txt

But for some reason I cant access methods of data within AssetData because of this error: You cannot call a method on a null-valued expression.

2 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    InhumaneTarball 

     

    Hi, Ivan.

     

    I'm not sure what method you're expecting to call, or from where, as the HTTPS response is simply plain text using JSON notation, and a plain text object has very few methods to begin with.

     

    ConvertFrom-Json is simply taking that JSON text and transforming it into PowerShell [pscustomobject] object(s) to make subsequent scripting a little easier, but there's no magic going on that can convert it into an actual class complete with methods.

     

    Typically, what happens where that kind of advanced functionality is required is that you will have a client-side PowerShell module that defines classes, which may or may not contains methods - but let's say they do. You will then invoke some commandlet that behind the scenes fetches the JSON via a HTTPS call and then locally instantiates a new instance(s) of that client-side class into which the JSON response is injected.

     

    You can see from looking at your HTTPS response that this doesn't apply. The base "object" as well as the properties (depending on depth) are simple [pscustomobject] types where no meaningful methods exist, meaning the error is almost certainly accurate.

     

     

    Cheers,

    Lain

  • InhumaneTarball I use this to install WinGet on a system:

     

    #Install WinGet, used https://learn.microsoft.com/en-us/windows/package-manager/winget/#install-winget-on-windows-sandbox
    $progressPreference = 'silentlyContinue'
    Write-Information "Downloading WinGet and its dependencies..."
    Invoke-WebRequest -Uri https://aka.ms/getwinget -OutFile 
    $env:temp\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle
    Invoke-WebRequest -Uri https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx -OutFile $env:temp\Microsoft.VCLibs.x64.14.00.Desktop.appx
    Invoke-WebRequest -Uri https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx -OutFile $env:temp\Microsoft.UI.Xaml.2.8.x64.appx
    Add-AppxPackage $env:temp\Microsoft.VCLibs.x64.14.00.Desktop.appx
    Add-AppxPackage $env:temp\Microsoft.UI.Xaml.2.8.x64.appx
    Add-AppxPackage $env:temp\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle

Resources