Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8715691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:54:31+00:00 2026-06-13T05:54:31+00:00

I have the following script, but I need to replace the Write-Host ‘s and

  • 0

I have the following script, but I need to replace the Write-Host‘s and store the output in a single variable rather than writing the output.

I want to end up with all the output in one variable that I can then email the contents of.

Write-Host $machines2 at the end of the loop lists each machine to the console.

# Start writing headers to output
write-host "        MACHINE  USER  TAG    EXPIRY DATE"

# Import SQL module for PowerShell
import-module SQLPS -DisableNameChecking

# Run first SQLquery to get machine name & servie tag
$data1 = Invoke-Sqlcmd -ServerInstance SERVER -Database DATABASE -Query “SELECT guid, name, [Serial Number] FROM dbo.wrksta as a inner join Inv_AeX_HW_Serial_Number as b on a.GUID=b._resourceGUID ORDER BY Name”;

# Iterate through results and apply logic to populate list
foreach ($row in $data1)
{
    # Set variables from Query result
    $name = $row.name;
    $serial = $row."Serial Number";

    # Get machine user
    $user = invoke-sqlcmd -ServerInstance SERVER -Database DATABASE -Query “SELECT top 1 [User] FROM dbo.Evt_aex_client_logon WHERE _resourceGuid = (SELECT top 1 Guid FROM dbo.Wrksta WHERE Name='$name' ORDER BY WrkstaID DESC) AND Event = 'logon' AND _eventTime > getdate()-60 GROUP BY [User] ORDER BY count([User]) desc” ;

    # Set machine user without column header
    $user2 = $user.User;

    # Set service tag from query data
    [String]$ServiceTag = $serial;

    Try
    {
        # Function to obtain XML information about a Dell system from a service tag from the Dell xserv site
        $AssetService = New-WebServiceProxy -Uri "http://xserv.dell.com/services/AssetService.asmx?WSDL";
        $ApplicationName = "AssetService";
        $Guid = [Guid]::NewGuid();
        $Asset = $AssetService.GetAssetInformation($Guid, $ApplicationName, $ServiceTag);
        $Writer = New-Object "System.IO.StringWriter";
        $XmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($Asset.GetType());
        $XmlSerializer.Serialize($Writer, $Asset);
        [String]$Date = $Writer.ToString();
        $Writer.Flush();
        $Writer.Close();
    }
    # Required exception block
    Catch
    {
        Write-Host $($_.Exception.Message);
    }

    # Match purchase date from returned data in incorrect format 2012-03-23
    $prog = [regex]::match($Date, '(?<=StartDate>)(.*)(?=T00)').Groups[1].Value

    # Edit the date to replace the - with /
    $prog2 = [System.Text.RegularExpressions.Regex]::Replace($prog, "[-]", "/");

    if ($prog2)
    {
        # Parse the value e.g. 2012/03/23 into a full datetime i.e. 23 March 2012 12:00:00
        $dt = [datetime]::ParseExact($prog2, "yyyy/MM/dd", $null)

        # Now string is in datetime format use the -format command to re-order and trim to our desired date 23/03/2012
        $purchased = Get-Date $dt -Format 'dd/MM/yyyy'

        # Store all variables in a table with relevant column headings
        $machines = New-Object PSObject -Property @{Name = $name; Serial = $Serial; User2 = $user2; purchased = $purchased}

        # Set what the date was 4 years ago today
        $fourYearsAgo = (Get-Date).AddYears(-4)

        # Compare our returned date to the date 4 years ago and see if it is less than or equal too i.e. in warranty
        $tobereplaced = $machines.purchased | Where-Object { (Get-Date $machines.purchased) -le $fourYearsAgo}

        if ($tobereplaced)
        {
            # Parse the date back to a full date/time
            $dt2 = [datetime]::ParseExact($tobereplaced, "dd/MM/yyyy", $null)

            # Add 4 years onto the date
            $replacementdate = (get-date $dt2).AddYears(+4)

            # Trim it back to our desired format
            $tbr = Get-Date $replacementdate -Format 'dd/MM/yyyy'

            # Build our final variable containing our data
            "EXPIRED" + " " + $name + " " + $user2 + " " + $serial + " " + $tbr;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:54:32+00:00Added an answer on June 13, 2026 at 5:54 am

    Don’t save your output to a variable, rather output in to the pipeline. Wrap your code into a function and collect whatever is returned, into a variable, similar to this:

    function abc()
    {
        $string1
        .... #processing block 1
        $string2
        .... #processing block 2
        $string3
        .... #even more processing.
    }
    

    If it is a set of strings, you can then join them together and send over the email:

    $mydata = abc;
    $myEmailBody = $mydata -join "`n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script with a UI Datepicker, but I need the date
ORIGINAL QUESTION: I have the following script: http://jsfiddle.net/mnXdv/12/ It works well, but I need
I have the following PHP script: <?php $fortune = `fortune`; echo $fortune; ?> but
I have written a Perl script for the following bioinformatics question, but unfortunately there
I have following small script to preview some text before submitting it to store
I have the following problem: I have a form I need to serialize but
I have the following script which runs fine but I think a) it is
I have the following script which I need to combine together somehow. Each function
I've got following page: <div><script>AddSomeContent(??, 1)</script></div> <div><script>AddSomeContent(??, 2)</script></div> I need to replace the ??
I have following script that executes all the .reg files in the current directory.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.