export the output

Rising Flight 3,751 Reputation points
2024-04-24T05:05:14.7+00:00

Hi all i want to execute the below syntax on few servers and i have created a collection and added the Servers to it. When i execute manually from powershell i get the output but i want to export that output to a text file under c:\windows\Temp folder. In SCCM from scripts i want to execute the below syntax but i am not sure how to export the output to a text file. please guide me.

New-Item 'C:\Windows\System32\config\imageconfig' -ItemType File -Value '{ image_name: "NONE", sharing_account: "NONE", viaGI: "NO" }'; Set-ItemProperty -Path 'C:\Windows\System32\config\imageconfig' -Name IsReadOnly -Value $true

ic

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Deployment: The process of delivering, assembling, and maintaining a particular version of a software system at a site.
906 questions
Microsoft Configuration Manager Application
Microsoft Configuration Manager Application
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Application: A computer program designed to carry out a specific task other than one relating to the operation of the computer itself, typically to be used by end users.
459 questions
Microsoft Configuration Manager
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,082 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Jing Zhou 2,240 Reputation points Microsoft Vendor
    2024-04-24T08:52:48.9433333+00:00

    Hello,

    Thank you for posting in Q&A forum.

    We suggest that you try using PowerShell's output redirection to export the command's output to a text file. In your script, you can add the>operator at the end of each command and specify the file path to save the output. For example:

    Powershell

    New Item 'C: \ Windows \ System32 \ config \ imageconfig' - ImageType File Value '{imagename: "NONE", sharingaaccount: "NONE", viaGI: "NO"}'>'C: \ Windows \ Temp-output. txt' Set itemProperty - Path 'C: \ Windows \ System32 \ config \ imageconfig' - Name IsReadOnly - Value $true>>'C: \ Windows \ Temp.output. txt'

    In this example, the>operator is used to overwrite the output file, and the>>operator is used to append the output to the end of the file. You can choose one as needed.

    In the SCCM script, you can directly embed the above commands into the script and specify the output file path as a location that the SCCM client can access. For example:

    Powershell

    New Item 'C: \ Windows \ System32 \ config \ imageconfig' - ImageType File Value '{imagename: "NONE", sharingaaccount: "NONE", viaGI: "NO"}'>'C: \ Windows \ Temp-output. txt' Set itemProperty - Path 'C: \ Windows \ System32 \ config \ imageconfig' - Name IsReadOnly - Value $true>>'C: \ Windows \ Temp.output. txt'

    This will execute the script on the SCCM client's computer and save the output to the specified text file.

    Hope this answer can help you well.

    Best regards,

    Jill Zhou

    0 comments No comments

  2. Rising Flight 3,751 Reputation points
    2024-04-24T15:19:32.2+00:00

    From SCCM when i deploy the below syntax using scripts on a collection. on the servers i see blank file(imageconfig-op.txt) in the path C:\Windows\temp.

    New-Item 'C:\Windows\System32\config\imageconfig' -ItemType File -Value '{ image_name: "NONE", sharing_account: "NONE", viaGI: "NO" }'>'C:\Windows\Temp\imageconfig-op.txt'; Set-ItemProperty -Path 'C:\Windows\System32\config\imageconfig' -Name IsReadOnly -Value $true>>'C:\Windows\Temp\imageconfig-op.txt'
    
    
    0 comments No comments

  3. Rich Matheisen 45,091 Reputation points
    2024-04-24T18:49:01.1133333+00:00

    Rewriting your script a little bit (just to separate the data from the process):

    $val = '{ image_name: "NONE", sharing_account: "NONE", viaGI: "NO" }'
    $filepath = 'C:\Windows\System32\config\imageconfig'
    $target = 'c:\windows\temp\imageconfig'
    
    New-Item $filepath -ItemType File -Value $val
    Set-ItemProperty -Path $filepath -Name IsReadOnly -Value $true
    Copy-Item $filepath -Destination $target -Force
    

    There's no error handling in that script and you don't say whether or not you're using a ErrorAction different to the default "Continue".

    I'd suspect you may have had a problem with the copying of the file using Copy-Item because the file name lacks a suffix and looks like a directory name, The -Force will "fix" that.


  4. Plamen Peev 0 Reputation points
    2024-04-30T19:56:44.52+00:00

    Hi @Rising Flight , just pipe the output of New-Item command to Out-File like so:

    New-Item 'C:\Windows\System32\config\imageconfig' -ItemType File -Value '{ image_name: "NONE", sharing_account: "NONE", viaGI: "NO" }' | Out-File "C:\Windows\Temp\output.txt" -Append; Set-ItemProperty -Path 'C:\Windows\System32\config\imageconfig' -Name IsReadOnly -Value $true
    

    Here is a little more structured version, easier in case you want to change the paths at some point:

    $FilePath = 'C:\Windows\System32\config\imageconfig'
    $OutFile = "C:\Windows\Temp\output.txt"
    $Content = '{ image_name: "NONE", sharing_account: "NONE", viaGI: "NO" }'
    
    New-Item $FilePath -ItemType File -Value $Content | Out-File $OutFile -Append
    Set-ItemProperty -Path $FilePath -Name IsReadOnly -Value $true