PowerShell script to fetch all the disposed assets based on portal specific

PowerShell script to fetch all the disposed assets based on portal specific

Steps to use the provided script:

  1. Ensure that the required modules are installed by executing the following commands:
    $ErrorActionPreference = "Stop"
    Install-Module -Name 'ImportExcel' -Force -AllowClobber
  2. Import the necessary modules by executing the following command:
    Import-Module -Name 'ImportExcel'
  3. Set up the script variables according to your requirements:
    • Update the $url variable with the appropriate API URL.
    • Replace the $api_key variable with your actual API key.
    • Adjust the $Portalid variable to match your desired portal ID.
    • Modify the $headers variable if needed.
  4. Run the script by executing the remaining code. The script will fetch asset data using the specified parameters, loop through the results, and store the relevant information in arrays.
  5. The script will check if a file named "Disposed_Asset.xlsx" already exists. If it does, it will increment a counter and append the counter value to the filename until a unique filename is found.
  6. A DataTable is created, and the asset data is populated in the table.
  7. The script will output a message indicating the filename where the "Disposed_Asset" data was saved.



Powershell script :
This document provides step-by-step instructions for running the provided PowerShell script on both Windows and Linux systems.

Prerequisites:

  1. PowerShell: Make sure PowerShell is installed on your system. Check the PowerShell version by running the command powershell -version. It should be PowerShell version 5.0 or later.

Windows:

  1. Open PowerShell: Launch PowerShell by searching for "PowerShell" in the Start menu, or by pressing Win + X and selecting "Windows PowerShell" or "PowerShell" from the menu.
  2. Set Execution Policy: If your system's execution policy does not allow running scripts, you need to change the execution policy. Run the following command in PowerShell with administrative privileges:
    Set-ExecutionPolicy RemoteSigned

  3. Install Required Module: The script requires the 'ImportExcel' module. Run the following command to install it:
    Install-Module -Name 'ImportExcel' -Force -AllowClobber

  4. Run the Script: Copy the provided script and paste it into the PowerShell window, then press Enter. The script will start running and fetch asset data. It will save the data to an Excel file with a unique name like "Disposed_Asset.xlsx" or "Disposed_Asset_2.xlsx" if the file already exists  # Paste the script here
  5. Verify Output: Once the script completes, it will display the path where the Excel file is saved, such as "Disposed_Asset data saved to C:\Path\to\Disposed_Asset.xlsx".

Linux:

  1. Open Terminal: Launch the terminal emulator on your Linux distribution. It can usually be found in the applications menu or by pressing Ctrl + Alt + T.
  2. Install PowerShell: If PowerShell is not installed on your Linux system, you need to install it. The installation steps may vary depending on your Linux distribution. Please refer to the official Microsoft documentation for instructions: Installing PowerShell on Linux
  3. Set Execution Policy: PowerShell on Linux requires explicitly setting the execution policy to run scripts. Run the following command in the terminal:
    sudo pwsh -Command "Set-ExecutionPolicy RemoteSigned"
    Enter your password when prompted.

  4. Install Required Module: The script requires the 'ImportExcel' module. Run the following command in the terminal:
    sudo pwsh -Command "Install-Module -Name 'ImportExcel' -Force -AllowClobber"
    Enter your password when prompted.

  5. Run the Script: Copy the provided script and paste it into the terminal, then press Enter. The script will start running and fetch asset data. It will save the data to an Excel file with a unique name like "Disposed_Asset.xlsx" or "Disposed_Asset_2.xlsx" if the file already exists.
    # Paste the script here
  6. Verify Output: Once the script completes, it will display the path where the Excel file is saved, such as "Disposed_Asset data saved to /path/to/Disposed_Asset.xlsx".

 

     MacOS:

  1. Open Terminal: Launch the Terminal application by going to "Applications" > "Utilities" > "Terminal" or by using Spotlight search (press Cmd + Space and type "Terminal").
  2. Check PowerShell Installation: Confirm if PowerShell is installed on your macOS system. In the Terminal, type pwsh and press Enter. If PowerShell is installed, the PowerShell prompt will appear. If it is not installed, you will need to install PowerShell for macOS. You can follow the official Microsoft documentation for instructions: Installing PowerShell on macOS
  3. Install Required Module: The script requires the 'ImportExcel' module. In the Terminal, type the following command and press Enter:
    sudo pwsh -Command "Install-Module -Name 'ImportExcel' -Force -AllowClobber"
    Enter your password when prompted.

  4. Run the Script: Copy the provided PowerShell script and paste it into the Terminal window, then press Enter. The script will start running and fetch asset data. It will save the data to an Excel file with a unique name like "Disposed_Asset.xlsx" or "Disposed_Asset_2.xlsx" if the file already exists.
    # Paste the script here
  5. Verify Output: Once the script completes, it will display the path where the Excel file is saved, such as "Disposed_Asset data saved to /path/to/Disposed_Asset.xlsx".

 




                  New to ADSelfService Plus?