how to create lnk with powershell and set working directory parameter
Assistant
Creating a shortcut (LNK file) using PowerShell can be done using the Windows Script Host (WSH). You can utilize the WScript.Shell COM object to create shortcuts and set parameters such as the target path and working directory.
User
Give me sql request for sccm to find all systems with certain software id installed
Assistant
To find all systems with a specific software installed in , you would typically query the SCCM database using SQL. The software inventory data is stored in the view (or the view depending on your SCCM version and setup).
Here’s a simple example of how to create a shortcut for a program with a specified working directory:
Example: Creating a Shortcut with PowerShell
Open PowerShell.
Use the following script, modifying the paths and names as needed:
# Define variables for shortcut properties
$shortcutPath = "$env:USERPROFILE\Desktop\YourShortcut.lnk" # Path where the shortcut will be created
$targetPath = "C:\Path\To\Your\Application.exe" # Path of the target application
$workingDirectory = "C:\Path\To\Your\Working\Directory" # Desired working directory
# Create a WScript.Shell COM object
$WshShell = New-Object -ComObject WScript.Shell
# Create the shortcut
$shortcut = $WshShell.CreateShortcut($shortcutPath)
# Set properties
$shortcut.TargetPath = $targetPath # Path to the application
$shortcut.WorkingDirectory = $workingDirectory # Set the working directory
$shortcut.IconLocation = $targetPath # Optional: set icon location
$shortcut.Save() # Save the shortcut
Explanation:
$shortcutPath: This is where you want to create the shortcut (e.g., desktop).
$targetPath: This is the path to the application you want to create a shortcut for.
$workingDirectory: This is the directory that should be set as the working directory when the shortcut is launched.
WScript.Shell: This COM object is used to create and manage Windows shortcuts.
CreateShortcut: This method creates the actual shortcut file.
TargetPath, WorkingDirectory, IconLocation: These properties define important behaviors and appearances of the shortcut.
Notes:
Ensure that you have the correct permissions to create files in the specified location.
Modify the paths as necessary to point to your application and desired locations.
After running the script, a .lnk file should appear at the specified location (e.g., on your desktop).
This way, you can create a shortcut quickly using PowerShell, along with the working directory set as specified.
System Center Configuration Manager (SCCM)
dbo.v_GS_INSTALLED_SOFTWARE
dbo.v_Add_Remove_Programs
Here’s a basic SQL query you can use:
SQL Query Example
SELECT
DISTINCT sys.Name0 AS [Computer Name],
sys.ResourceID,
sw.ProductName0 AS [Software Name],
sw.Version0 AS [Software Version]
FROM
dbo.v_GS_COMPUTER_SYSTEM AS sys
JOIN
dbo.v_GS_INSTALLED_SOFTWARE AS sw ON sys.ResourceID = sw.ResourceID
WHERE
sw.ProductID0 = 'YOUR_SOFTWARE_ID' -- Replace with your software ID or ProductName
ORDER BY
sys.Name0
Explanation:
SELECT DISTINCT: This ensures that you only retrieve unique computer names even if multiple software entries exist for them.
sys.Name0: This is the computer name.
sys.ResourceID: This is the unique identifier for the system in SCCM.
sw.ProductName0: This is the name of the installed software.
sw.Version0: This gives the version of the installed software.
FROM dbo.v_GS_COMPUTER_SYSTEM: This view contains the computer systems managed by SCCM.
JOIN dbo.v_GS_INSTALLED_SOFTWARE: This joins to the installed software view to get details about the software installed on each computer.
WHERE sw.ProductID0 = 'YOUR_SOFTWARE_ID': Replace 'YOUR_SOFTWARE_ID' with the actual software ID you are querying for. If you know the product name instead, you might use sw.ProductName0 LIKE '%Your Software Name%'.
Note:
Finding Specific Software: If you're unsure of the software ID, you can find it by querying the dbo.v_GS_INSTALLED_SOFTWARE table with a more general condition and then adjusting your query based on that output. You could list all installed software:
SELECT DISTINCT
sw.ProductID0,
sw.ProductName0
FROM
dbo.v_GS_INSTALLED_SOFTWARE AS sw
ORDER BY
sw.ProductName0
Important:
Ensure you have appropriate permissions to run SQL queries against the SCCM database.
Test your SQL queries in a safe environment before running them in production.
Connect to the SCCM database through tools like SQL Server Management Studio (SSMS).
This SQL query should help you to find all systems with the particular software installed. Adjust the filtering as necessary based on your requirements.