Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Export-CSV as a different user

$
0
0

Wrote a sript to audit a few reg keys and other vitals from the computer.  Eventually I will have this go to a database but in the meantime I am trying to write it to a text file.  These are mobile computers that log in as a generic local user.

The powershell script as is needs to write to a domain file and therefore doesnt have permissions.  Can someone help me use different creds within the powershell script to use for access?

The Export-CSV function is in use for the append switch as these are powershell v2

#Get Local User SID for use as primary key
$CS = Gwmi Win32_ComputerSystem -Comp "."
$CS.UserName
$objUser = New-Object System.Security.Principal.NTAccount($CS.UserName)
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
#$strSID.value
#Get Versions
$SoftwareVersionKey = 'HKLM:\SOFTWARE\Wow6432Node\Software1\Portal'
$SoftwarePackageVersion = (Get-ItemProperty -path $Software1VersionKey).VERSION1
$Software1Version = (Get-ItemProperty -path $Software1VersionKey).Version1
#$Software1PackageVersion
#$Software1Version
#Get Software1 GUID
$Software1Key = 'HKLM:\SOFTWARE\Wow6432Node\Software1'
$Software1GUID = (Get-ItemProperty -path $Software1Key).GUID
#$Software1GUID
#Get Software1 Datasync Identity
$Software1DSIdentKey = 'HKLM:\SOFTWARE\Wow6432Node\Software1\SyncAgent'
$Software1Identity = (Get-ItemProperty -path $Software1DSIdentKey).Identity
#$Software1Identity
#Get Computer Information / Computer Name / Installed Memory (MB)
$WMI = Get-WmiObject -Class Win32_ComputerSystem
$computername = ($WMI.name)
$computerDomain = ($WMI.Domain)
$computerManufacturer = ($WMI.Manufacturer)
$computerModel = ($WMI.Model)
$InstalledMemory = ($WMI.TotalPhysicalMemory/1mb)
#Get Computer Information / Computer Name / Installed Memory (MB)
$WMIBIOS = Get-WmiObject -Class Win32_BIOS
$BIOSVersion1 = ($WMIBIOS.SMBIOSBIOSVersion)
$BIOSVersion2 = ($WMIBIOS.Version)
$computerSerial = ($WMIBIOS.SerialNumber)
#Date
$date = Get-Date -format "yyyyMMdd"
function Export-CSV {
[CmdletBinding(DefaultParameterSetName='Delimiter',
  SupportsShouldProcess=$true, ConfirmImpact='Medium')]
param(
 [Parameter(Mandatory=$true, ValueFromPipeline=$true,
           ValueFromPipelineByPropertyName=$true)]
 [System.Management.Automation.PSObject]
 ${InputObject},
 [Parameter(Mandatory=$true, Position=0)]
 [Alias('PSPath')]
 [System.String]
 ${Path},
 [Switch]
 ${Append},
 [Switch]
 ${Force},
 [Switch]
 ${NoClobber},
 [ValidateSet('Unicode','UTF7','UTF8','ASCII','UTF32',
                  'BigEndianUnicode','Default','OEM')]
 [System.String]
 ${Encoding},
 [Parameter(ParameterSetName='Delimiter', Position=1)]
 [ValidateNotNull()]
 [System.Char]
 ${Delimiter},
 [Parameter(ParameterSetName='UseCulture')]
 [Switch]
 ${UseCulture},
 [Alias('NTI')]
 [Switch]
 ${NoTypeInformation})
begin
{
 $AppendMode = $false
 try {
  $outBuffer = $null
  if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
  {
      $PSBoundParameters['OutBuffer'] = 1
  }
  $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Export-Csv',
    [System.Management.Automation.CommandTypes]::Cmdlet)
 $scriptCmdPipeline = ''
 if ($Append) {
  $PSBoundParameters.Remove('Append') | Out-Null
  if ($Path) {
   if (Test-Path $Path) {        
    $AppendMode = $true
    if ($Encoding.Length -eq 0) {
     $Encoding = 'ASCII'
    }
    $scriptCmdPipeline += 'ConvertTo-Csv -NoTypeInformation '
    if ( $UseCulture ) {
     $scriptCmdPipeline += ' -UseCulture '
    }
    if ( $Delimiter ) {
     $scriptCmdPipeline += " -Delimiter '$Delimiter' "
    } 
    $scriptCmdPipeline += ' | Foreach-Object {$start=$true}'
    $scriptCmdPipeline += '{if ($start) {$start=$false} else {$_}} '
    $scriptCmdPipeline += " | Out-File -FilePath '$Path'"
    $scriptCmdPipeline += " -Encoding '$Encoding' -Append "
    if ($Force) {
     $scriptCmdPipeline += ' -Force'
    }
    if ($NoClobber) {
     $scriptCmdPipeline += ' -NoClobber'
    }   
   }
  }
 } 
 $scriptCmd = {& $wrappedCmd @PSBoundParameters }
 if ( $AppendMode ) {
  $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
      $scriptCmdPipeline
    )
 } else {
  $scriptCmd = $ExecutionContext.InvokeCommand.NewScriptBlock(
      [string]$scriptCmd
    )
 }
 $steppablePipeline = $scriptCmd.GetSteppablePipeline(
        $myInvocation.CommandOrigin)
 $steppablePipeline.Begin($PSCmdlet)
 } catch {
   throw
 }
}
process
{
  try {
      $steppablePipeline.Process($_)
  } catch {
      throw
  }
}
end
{
  try {
      $steppablePipeline.End()
  } catch {
      throw
  }
}
}
New-Object -TypeName PSCustomObject -Property @{
    UserSID = $strSID.value
    ComputerName = $computername
    Domain = $computerDomain
    ComputerManufactor = $computerManufacturer
    ComputerModel = $computerModel
    ComputerSerial = $computerSerial
    InstallMemory = $InstalledMemory
    BIOS1 = $BIOSVersion1
    BIOS2 = $BIOSVersion2
    Software1PackageVersion = $Software1PackageVersion
    Software1Version = $Software1Version
    Software1GUID = $Software1GUID
    Software1Identity = $Software1Identity
    Date = $date
    } | Export-Csv -Path '\\server\drive\IT Shared\Audittest.csv' -NoTypeInformation -Append


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>