Need powershell script to update the SQL database

Welcome Forums General PowerShell Q&A Need powershell script to update the SQL database

Viewing 3 reply threads
  • Author
    Posts
    • #183303
      Participant
      Topics: 2
      Replies: 2
      Points: 21
      Rank: Member

      <p style=”margin: 0in; margin-bottom: .0001pt;”>  SQL command:</p>
      <p style=”margin: 0in; margin-bottom: .0001pt;”>  update [Ascertain_PROD].dbo.HostStatus</p>
      <p style=”margin: 0in; margin-bottom: .0001pt;”>  set Comments = ‘Decommissioned’ where HostID in (SELECT  H.[HostID]</p>
      <p style=”margin: 0in; margin-bottom: .0001pt;”>  FROM [Ascertain_PROD].[dbo].[Host] H left join [Ascertain_PROD].dbo.HostStatus HS on H.HostID = HS.HostID</p>
      <p style=”margin: 0in; margin-bottom: .0001pt;”>  where h.HostName = ‘XXXXXServerNameXXXXX’)</p>
      We need to create a powershell script using above update sql command to run remotely to update the database . Once run the script, server will be updated as decommissioned in our inventory and it should run using credentails. please help me to get the script to update.

      Example:

    • #183447
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      Hi Sekhar,

      The script looks good to me. What is the issue here?

      Thank you.

      • #183483
        Participant
        Topics: 2
        Replies: 2
        Points: 21
        Rank: Member

        When I tried to run this script on the SQL server. it’s throwing an error as function ExecuteSqlQuery is not recognized .

    • #183513
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      Yes, call the function after the function is created.

       

    • #183654
      Participant
      Topics: 10
      Replies: 41
      Points: 213
      Helping Hand
      Rank: Participant

      Possible simplification – the SqlServer module has Invoke-Sqlcmd.

Viewing 3 reply threads
  • The topic ‘Need powershell script to update the SQL database’ is closed to new replies.